Python基础(十二),十二大基础能力
Python基础(十二),十二大基础能力
今日主要内容
- 推导式
- 生成器表达式
- lambda匿名函数
- 内置函数介绍
一、推导式
(一)列表推导式
先来看一段代码
- 建立一个空列表,向空列表中添加元素
lst = list()
for i in range(5):
lst.append(i)
print(lst)
运行结果:
[0, 1, 2, 3, 4]
- 利用列表推导式,效果一样
lst = [i for i in range(5)]
print(lst)
运行结果:
[0, 1, 2, 3, 4]
(二)字典推导式
同理,先来看一段代码
- 建立一个空字典,向字典中添加键值对
dic = dict()
for i in range(1, 5):
dic[f"k{i}"] = i
print(dic)
运行结果:
{'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4}
- 使用字典推导式,效果一样
dic = {f"k{i}": i for i in range(1, 5)}
print(dic)
运行结果:
{'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4}
(三)集合推导式
- 与列表一样,这里就不再举例了
(四)推导式应用
- 来看几个推导式的应用例题
二、生成器表达式
(一)回顾:通过函数实现生成器
回顾一下通过函数实现生成器
- 将函数
return
变成yield
就变成了生成器
def func():
yield 1
yield 2
yield 3
g = func()
print(next(g))
print(next(g))
print(next(g))
运行结果:
1 2 3
(二)生成器表达式
三、lambda匿名函数
(一)匿名函数定义
(二)结合匿名函数的列表推导式
例一:
- 列表推导式:
g = [lambda :i+1 for i in range(3)]
print([em() for em in g])
运行结果:
[3, 3, 3]
- 转换为普通代码
g = list()
for i in range(3):
def func():
return i + 1
g.append(func)
new_g = list()
for em in g:
new_g.append(em())
print(new_g)
运行结果:
[3, 3, 3]
例二:
- 列表推导式:
g = [lambda i:i+1 for i in range(3)]
print([em(3) for em in g])
运行结果:
[4, 4, 4]
- 转换为普通代码
g = list()
for i in range(3):
def func(i):
return i + 1
g.append(func)
new_g = list()
for em in g:
new_g.append(em(3))
print(new_g)
运行结果:
[4, 4, 4]
(三)结合匿名函数的生成器表达式
例一:
- 生成器表达式:
g = (lambda i:i+1 for i in range(3))
print([em(3) for em in g])
运行结果:
[4, 4, 4]
- 转换为函数实现的生成器:
def g():
for i in range(3):
def func(i):
return i + 1
yield func
new_g = list()
for em in g():
new_g.append(em(3))
print(new_g)
运行结果:
[4, 4, 4]
例二:
- 生成器表达式:
g = (lambda :i+1 for i in range(3))
print([em() for em in g])
运行结果:
[1, 2, 3]
- 转换为函数实现的生成器:
def g():
for i in range(3):
def func():
return i + 1
yield func
new_g = list()
for em in g():
new_g.append(em())
print(new_g)
运行结果:
[1, 2, 3]
例三:
- 生成器表达式
g = (lambda x:x*i for i in range(3))
for j in [2,10]:
g1 = (em(3) for em in g)
print([e+j for e in g1])
运行结果:
[10, 13, 16]
- 转换为普通代码
def g():
for i in range(3):
def func(x):
return x * i
yield func
for j in [2, 10]:
def g1():
for em in g():
yield em(3)
new_g = list()
for e in g1():
new_g.append(e + j)
print(new_g)
运行结果:
[10, 13, 16]
四、内置函数
查看解释器内置常量和内置函数的方法
dir(__builtins__)
Python3.6.8的内置函数(共72个)
abs
all
any
ascii
bin
bool
bytearray
bytes
callable
chr
classmethod
compile
complex
copyright
credits
delattr
dict
dir
divmod
enumerate
eval
exec
exit
filter
float
format
frozenset
getattr
globals
hasattr
hash
help
hex
id
input
int
isinstance
issubclass
iter
len
license
list
locals
map
max
memoryview
min
next
object
oct
open
ord
pow
print
property
quit
range
repr
reversed
round
set
setattr
slice
sorted
staticmethod
str
sum
super
tuple
type
vars
zip
(一)基础函数(了解)
all()
any()
bytes()
callable()
chr()
ord()
complex()
divmid()
eval()
exec()
frozenset()
help()
globals()
locals()
hash()
id()
iter()
next()
bin()
oct()
int()
hex()
pow()
repr()
round()
bool()
(二)常用函数(会用)
abs()
format()
enumerate()
open()
range()
print()
input()
len()
list()
dict()
str()
set()
tuple()
float()
reversed
sum()
dir()
type()
zip()
bool()
(三)高阶函数(熟练)
高阶函数全部需要结合规则函数,利用lambda匿名函数构造规则函数
filter()
筛选过滤
map()
映射
sorted()
排序
max()
求最大值
min()
求最小值
reduce()
累计算
(四)面向对象函数
classmethod()
delattr()
getattr()
hasattr()
issubclass()
isinstance()
object()
property()
setattr()
staticmethod()
super()
- 后续补充...
(一)列表推导式
先来看一段代码
- 建立一个空列表,向空列表中添加元素
lst = list() for i in range(5): lst.append(i) print(lst) 运行结果: [0, 1, 2, 3, 4]
- 利用列表推导式,效果一样
lst = [i for i in range(5)] print(lst) 运行结果: [0, 1, 2, 3, 4]
(二)字典推导式
同理,先来看一段代码
- 建立一个空字典,向字典中添加键值对
dic = dict() for i in range(1, 5): dic[f"k{i}"] = i print(dic) 运行结果: {'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4}
- 使用字典推导式,效果一样
dic = {f"k{i}": i for i in range(1, 5)} print(dic) 运行结果: {'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4}
(三)集合推导式
- 与列表一样,这里就不再举例了
(四)推导式应用
- 来看几个推导式的应用例题
二、生成器表达式
(一)回顾:通过函数实现生成器
回顾一下通过函数实现生成器
- 将函数
return
变成yield
就变成了生成器
def func():
yield 1
yield 2
yield 3
g = func()
print(next(g))
print(next(g))
print(next(g))
运行结果:
1 2 3
(二)生成器表达式
三、lambda匿名函数
(一)匿名函数定义
(二)结合匿名函数的列表推导式
例一:
- 列表推导式:
g = [lambda :i+1 for i in range(3)]
print([em() for em in g])
运行结果:
[3, 3, 3]
- 转换为普通代码
g = list()
for i in range(3):
def func():
return i + 1
g.append(func)
new_g = list()
for em in g:
new_g.append(em())
print(new_g)
运行结果:
[3, 3, 3]
例二:
- 列表推导式:
g = [lambda i:i+1 for i in range(3)]
print([em(3) for em in g])
运行结果:
[4, 4, 4]
- 转换为普通代码
g = list()
for i in range(3):
def func(i):
return i + 1
g.append(func)
new_g = list()
for em in g:
new_g.append(em(3))
print(new_g)
运行结果:
[4, 4, 4]
(三)结合匿名函数的生成器表达式
例一:
- 生成器表达式:
g = (lambda i:i+1 for i in range(3))
print([em(3) for em in g])
运行结果:
[4, 4, 4]
- 转换为函数实现的生成器:
def g():
for i in range(3):
def func(i):
return i + 1
yield func
new_g = list()
for em in g():
new_g.append(em(3))
print(new_g)
运行结果:
[4, 4, 4]
例二:
- 生成器表达式:
g = (lambda :i+1 for i in range(3))
print([em() for em in g])
运行结果:
[1, 2, 3]
- 转换为函数实现的生成器:
def g():
for i in range(3):
def func():
return i + 1
yield func
new_g = list()
for em in g():
new_g.append(em())
print(new_g)
运行结果:
[1, 2, 3]
例三:
- 生成器表达式
g = (lambda x:x*i for i in range(3))
for j in [2,10]:
g1 = (em(3) for em in g)
print([e+j for e in g1])
运行结果:
[10, 13, 16]
- 转换为普通代码
def g():
for i in range(3):
def func(x):
return x * i
yield func
for j in [2, 10]:
def g1():
for em in g():
yield em(3)
new_g = list()
for e in g1():
new_g.append(e + j)
print(new_g)
运行结果:
[10, 13, 16]
四、内置函数
查看解释器内置常量和内置函数的方法
dir(__builtins__)
Python3.6.8的内置函数(共72个)
abs
all
any
ascii
bin
bool
bytearray
bytes
callable
chr
classmethod
compile
complex
copyright
credits
delattr
dict
dir
divmod
enumerate
eval
exec
exit
filter
float
format
frozenset
getattr
globals
hasattr
hash
help
hex
id
input
int
isinstance
issubclass
iter
len
license
list
locals
map
max
memoryview
min
next
object
oct
open
ord
pow
print
property
quit
range
repr
reversed
round
set
setattr
slice
sorted
staticmethod
str
sum
super
tuple
type
vars
zip
(一)基础函数(了解)
all()
any()
bytes()
callable()
chr()
ord()
complex()
divmid()
eval()
exec()
frozenset()
help()
globals()
locals()
hash()
id()
iter()
next()
bin()
oct()
int()
hex()
pow()
repr()
round()
bool()
(二)常用函数(会用)
abs()
format()
enumerate()
open()
range()
print()
input()
len()
list()
dict()
str()
set()
tuple()
float()
reversed
sum()
dir()
type()
zip()
bool()
(三)高阶函数(熟练)
高阶函数全部需要结合规则函数,利用lambda匿名函数构造规则函数
filter()
筛选过滤
map()
映射
sorted()
排序
max()
求最大值
min()
求最小值
reduce()
累计算
(四)面向对象函数
classmethod()
delattr()
getattr()
hasattr()
issubclass()
isinstance()
object()
property()
setattr()
staticmethod()
super()
- 后续补充...
回顾一下通过函数实现生成器
- 将函数
return
变成yield
就变成了生成器
def func():
yield 1
yield 2
yield 3
g = func()
print(next(g))
print(next(g))
print(next(g))
运行结果:
1 2 3
(一)匿名函数定义
(二)结合匿名函数的列表推导式
例一:
- 列表推导式:
g = [lambda :i+1 for i in range(3)] print([em() for em in g]) 运行结果: [3, 3, 3]
- 转换为普通代码
g = list() for i in range(3): def func(): return i + 1 g.append(func) new_g = list() for em in g: new_g.append(em()) print(new_g) 运行结果: [3, 3, 3]
例二:
- 列表推导式:
g = [lambda i:i+1 for i in range(3)] print([em(3) for em in g]) 运行结果: [4, 4, 4]
- 转换为普通代码
g = list() for i in range(3): def func(i): return i + 1 g.append(func) new_g = list() for em in g: new_g.append(em(3)) print(new_g) 运行结果: [4, 4, 4]
(三)结合匿名函数的生成器表达式
例一:
- 生成器表达式:
g = (lambda i:i+1 for i in range(3)) print([em(3) for em in g]) 运行结果: [4, 4, 4]
- 转换为函数实现的生成器:
def g(): for i in range(3): def func(i): return i + 1 yield func new_g = list() for em in g(): new_g.append(em(3)) print(new_g) 运行结果: [4, 4, 4]
例二:
- 生成器表达式:
g = (lambda :i+1 for i in range(3)) print([em() for em in g]) 运行结果: [1, 2, 3]
- 转换为函数实现的生成器:
def g(): for i in range(3): def func(): return i + 1 yield func new_g = list() for em in g(): new_g.append(em()) print(new_g) 运行结果: [1, 2, 3]
例三:
- 生成器表达式
g = (lambda x:x*i for i in range(3)) for j in [2,10]: g1 = (em(3) for em in g) print([e+j for e in g1]) 运行结果: [10, 13, 16]
- 转换为普通代码
def g(): for i in range(3): def func(x): return x * i yield func for j in [2, 10]: def g1(): for em in g(): yield em(3) new_g = list() for e in g1(): new_g.append(e + j) print(new_g) 运行结果: [10, 13, 16]
四、内置函数
查看解释器内置常量和内置函数的方法
dir(__builtins__)
Python3.6.8的内置函数(共72个)
abs
all
any
ascii
bin
bool
bytearray
bytes
callable
chr
classmethod
compile
complex
copyright
credits
delattr
dict
dir
divmod
enumerate
eval
exec
exit
filter
float
format
frozenset
getattr
globals
hasattr
hash
help
hex
id
input
int
isinstance
issubclass
iter
len
license
list
locals
map
max
memoryview
min
next
object
oct
open
ord
pow
print
property
quit
range
repr
reversed
round
set
setattr
slice
sorted
staticmethod
str
sum
super
tuple
type
vars
zip
(一)基础函数(了解)
all()
any()
bytes()
callable()
chr()
ord()
complex()
divmid()
eval()
exec()
frozenset()
help()
globals()
locals()
hash()
id()
iter()
next()
bin()
oct()
int()
hex()
pow()
repr()
round()
bool()
(二)常用函数(会用)
abs()
format()
enumerate()
open()
range()
print()
input()
len()
list()
dict()
str()
set()
tuple()
float()
reversed
sum()
dir()
type()
zip()
bool()
(三)高阶函数(熟练)
高阶函数全部需要结合规则函数,利用lambda匿名函数构造规则函数
filter()
筛选过滤
map()
映射
sorted()
排序
max()
求最大值
min()
求最小值
reduce()
累计算
(四)面向对象函数
classmethod()
delattr()
getattr()
hasattr()
issubclass()
isinstance()
object()
property()
setattr()
staticmethod()
super()
- 后续补充...
查看解释器内置常量和内置函数的方法
dir(__builtins__)
Python3.6.8的内置函数(共72个)
abs | all | any | ascii | bin | bool |
---|---|---|---|---|---|
bytearray | bytes | callable | chr | classmethod | compile |
complex | copyright | credits | delattr | dict | dir |
divmod | enumerate | eval | exec | exit | filter |
float | format | frozenset | getattr | globals | hasattr |
hash | help | hex | id | input | int |
isinstance | issubclass | iter | len | license | list |
locals | map | max | memoryview | min | next |
object | oct | open | ord | pow | |
property | quit | range | repr | reversed | round |
set | setattr | slice | sorted | staticmethod | str |
sum | super | tuple | type | vars | zip |
all() | any() | bytes() | callable() |
---|---|---|---|
chr() | ord() | complex() | divmid() |
eval() | exec() | frozenset() | help() |
globals() | locals() | hash() | id() |
iter() | next() | bin() | oct() |
int() | hex() | pow() | repr() |
round() | bool() |
abs() | format() | enumerate() | open() |
---|---|---|---|
range() | print() | input() | len() |
list() | dict() | str() | set() |
tuple() | float() | reversed | sum() |
dir() | type() | zip() | bool() |
高阶函数全部需要结合规则函数,利用lambda匿名函数构造规则函数
filter() | 筛选过滤 |
---|---|
map() | 映射 |
sorted() | 排序 |
max() | 求最大值 |
min() | 求最小值 |
reduce() | 累计算 |
classmethod() | delattr() | getattr() |
---|---|---|
hasattr() | issubclass() | isinstance() |
object() | property() | setattr() |
staticmethod() | super() |
相关内容
- 暂无相关文章
评论关闭