day4-python之内置函数和匿名函数
分类:
IT文章
•
2023-11-30 12:55:07
一、内置函数
截止到python版本3.6.2,python为我们提供了68个内置函数。这些函数是python提供直接可以拿来使用的所有函数。
abs() |
dict() |
help() |
min() |
setattr() |
all() |
dir() |
hex() |
next() |
slice() |
any() |
divmod() |
id() |
object() |
sorted() |
ascii() |
enumerate() |
input() |
oct() |
staticmethod() |
bin() |
eval() |
int() |
open() |
str() |
bool() |
exec() |
isinstance() |
ord() |
sum() |
bytearray() |
filter() |
issubclass() |
pow() |
super() |
bytes() |
float() |
iter() |
print() |
tuple() |
callable() |
format() |
len() |
property() |
type() |
chr() |
frozenset() |
list() |
range() |
vars() |
classmethod() |
getattr() |
locals() |
repr() |
zip() |
compile() |
globals() |
map() |
reversed() |
__import__() |
complex() |
hasattr() |
max() |
round() |
|
delattr() |
hash() |
memoryview() |
set() |
|
把这些函数分成6大类:
1、作用域相关
基于字典的形式获取局部变量和全局变量
globals()——获取全局变量的字典
locals()——获取执行本方法所在命名空间内的局部变量的字典
1 a = 1
2 b = 2
3 print(locals())
4 print(globals())
5 # 这两个一样,因为是在全局执行的。
6
7 ##########################
8
9 def func(argv):
10 c = 2
11 print(locals())
12 print(globals())
13 func(3)
14
15 #这两个不一样,locals() {'argv': 3, 'c': 2}
16 #globals() {'__doc__': None, '__builtins__': <module 'builtins' (built-in)>, '__cached__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000024409148978>, '__spec__': None, '__file__': 'D:/lnh.python/.../内置函数.py', 'func': <function func at 0x0000024408CF90D0>, '__name__': '__main__', '__package__': None}
代码示例
2、其他
1)字符串类型代码的执行
eval() 将字符串类型的代码执行并返回结果
exec() 将字符串类型的代码执行
1 print(exec("1+2+3+4"))
2 exec("print('hello,world')")
1 code = '''
2 import os
3 print(os.path.abspath('.'))
4 '''
5 code = '''
6 print(123)
7 a = 20
8 print(a)
9 '''
10 a = 10
11 exec(code,{'print':print},)
12 print(a)
指定global参数
compile 将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。
参数说明:
1. 参数source:字符串或者AST(Abstract Syntax Trees)对象。即需要动态执行的代码段。
2. 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。当传入了source参数时,filename参数传入空字符即可。
3. 参数model:指定编译代码的种类,可以指定为 ‘exec’,’eval’,’single’。当source中包含流程语句时,model应指定为‘exec’;当source中只包含一个简单的求值表达式,model应指定为‘eval’;当source中包含了交互式命令语句,model应指定为'single'。
1 >>> #流程语句使用exec
2 >>> code1 = 'for i in range(0,10): print (i)'
3 >>> compile1 = compile(code1,'','exec')
4 >>> exec (compile1)
5 3
6 7
7
8
9 >>> #简单求值表达式用eval
10 >>> code2 = '1 + 2 + 3 + 4'
11 >>> compile2 = compile(code2,'','eval')
12 >>> eval(compile2)
13
14
15 >>> #交互语句用single
16 >>> code3 = 'name = input("please input your name:")'
17 >>> compile3 = compile(code3,'','single')
18 >>> name #执行前name变量不存在
19 Traceback (most recent call last):
20 File "<pyshell#29>", line 1, in <module>
21 name
22 NameError: name 'name' is not defined
23 >>> exec(compile3) #执行时显示交互命令,提示输入
24 please input your name:'pythoner'
25 >>> name #执行后name变量有值
26 "'pythoner'"
2)输入输出相关
input() 输入
1 s = input("请输入内容 : ") #输入的内容赋值给s变量
2 print(s) #输入什么打印什么。数据类型是str
input的用法
print 输出
1 def print(self, *args, sep=' ', end='
', file=None): # known special case of print
2 """
3 print(value, ..., sep=' ', end='
', file=sys.stdout, flush=False)
4 file: 默认是输出到屏幕,如果设置为文件句柄,输出到文件
5 sep: 打印多个值之间的分隔符,默认为空格
6 end: 每一次打印的结尾,默认为换行符
7 flush: 立即把内容输出到流文件,不作缓存
8 """
print源码剖析
1 f = open('tmp_file','w')
2 print(123,456,sep=',',file = f,flush=True)
file关键字的说明
1 import time
2 for i in range(0,101,2):
3 time.sleep(0.1)
4 char_num = i//2 #打印多少个'*'
5 per_str = '
%s%% : %s
' % (i, '*' * char_num) if i == 100 else '
%s%% : %s'%(i,'*'*char_num)
6 print(per_str,end='', flush=True)
7 #注意 :
可以把光标移动到行首但不换行
打印进度条
3)内存相关
id(o) o是参数,返回一个变量的内存地址
hash(o) o是参数,返回一个可hash变量的哈希值,不可hash的变量被hash之后会报错。
1 t = (1,2,3)
2 l = [1,2,3]
3 print(hash(t)) #可hash
4 print(hash(l)) #会报错
5
6 '''
7 结果:
8 TypeError: unhashable type: 'list'
9 '''
hash实例
hash函数会根据一个内部的算法对当前可hash变量进行处理,返回一个int数字。
*每一次执行程序,内容相同的变量hash值在这一次执行过程中不会发生改变。
4)文件操作相关
open() 打开一个文件,返回一个文件操作符(文件句柄)
操作文件的模式有r,w,a,r+,w+,a+ 共6种,每一种方式都可以用二进制的形式操作(rb,wb,ab,rb+,wb+,ab+)
可以用encoding指定编码。
5)模块相关
__import__导入一个模块
1 # 方法1
2 import time
3
4 # 方法2
5 os = __import__('os')
6 print(os.path.abspath('.'))
导入模块
6)帮助
在控制台执行help()进入帮助模式。可以随意输入变量或者变量的类型。输入q退出
或者直接执行help(o),o是参数,查看和变量o有关的操作。。。
7)调用相关
callable(o),o是参数,看这个变量是不是可调用。
如果o是一个函数名,就会返回True
1 def func():pass
2 print(callable(func)) #参数是函数名,可调用,返回True
3 print(callable(123)) #参数是数字,不可调用,返回False
callable实例
8)查看内置属性
查看参数所属类型的所有内置方法
dir() 默认查看全局空间内的属性,也接受一个参数,查看这个参数内的方法或变量
1 print(dir(list)) #查看列表的内置方法
2 print(dir(int)) #查看整数的内置方法
查看某变量/数据类型的内置方法
3、基础数据类型相关
1)和数字相关
数字——数据类型相关:bool,int,float,complex
bool :用于将给定参数转换为布尔类型,如果没有参数,返回 False。
int:函数用于将一个字符串或数字转换为整型。
1 print(int()) # 0
2
3 print(int('12')) # 12
4
5 print(int(3.6)) # 3
6
7 print(int('0100',base=2)) # 将2进制的 0100 转化成十进制。结果为 4
View Code
float:函数用于将整数和字符串转换成浮点数。
complex:函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。。
1 >>>complex(1, 2)
2 (1 + 2j)
3
4 >>> complex(1) # 数字
5 (1 + 0j)
6
7 >>> complex("1") # 当做字符串处理
8 (1 + 0j)
9
10 # 注意:这个地方在"+"号两边不能有空格,也就是不能写成"1 + 2j",应该是"1+2j",否则会报错
11 >>> complex("1+2j")
12 (1 + 2j)
View Code
数字——进制转换相关:bin,oct,hex
bin:将十进制转换成二进制并返回。
oct:将十进制转化成八进制字符串并返回。
hex:将十进制转化成十六进制字符串并返回。
1 print(bin(10),type(bin(10))) # 0b1010 <class 'str'>
2 print(oct(10),type(oct(10))) # 0o12 <class 'str'>
3 print(hex(10),type(hex(10))) # 0xa <class 'str'>
View Code
数字——数学运算:abs,divmod,min,max,sum,round,pow
abs:函数返回数字的绝对值。
divmod:计算除数与被除数的结果,返回一个包含商和余数的元组(a // b, a % b)。
round:保留浮点数的小数位数,默认保留整数。
pow:求x**y次幂。(三个参数为x**y的结果对z取余)
1 print(abs(-5)) # 5
2
3 print(divmod(7,2)) # (3, 1)
4
5 print(round(7/3,2)) # 2.33
6 print(round(7/3)) # 2
7 print(round(3.32567,3)) # 3.326
8
9 print(pow(2,3)) # 两个参数为2**3次幂
10 print(pow(2,3,3)) # 三个参数为2**3次幂,对3取余。
View Code
sum:对可迭代对象进行求和计算(可设置初始值)。
min:返回可迭代对象的最小值(可加key,key为函数名,通过函数的规则,返回最小值)。
max:返回可迭代对象的最大值(可加key,key为函数名,通过函数的规则,返回最大值)。
1 print(sum([1,2,3]))
2 print(sum((1,2,3),100))
3
4 print(min([1,2,3])) # 返回此序列最小值
5
6 ret = min([1,2,-5,],key=abs) # 按照绝对值的大小,返回此序列最小值
7 print(ret)
8
9 dic = {'a':3,'b':2,'c':1}
10 print(min(dic,key=lambda x:dic[x]))
11 # x为dic的key,lambda的返回值(即dic的值进行比较)返回最小的值对应的键
12
13
14 print(max([1,2,3])) # 返回此序列最大值
15
16 ret = max([1,2,-5,],key=abs) # 按照绝对值的大小,返回此序列最大值
17 print(ret)
18
19 dic = {'a':3,'b':2,'c':1}
20 print(max(dic,key=lambda x:dic[x]))
21 # x为dic的key,lambda的返回值(即dic的值进行比较)返回最大的值对应的键
View Code
2)和数据结构相关
序列——列表和元组相关的:list和tuple
list:将一个可迭代对象转化成列表(如果是字典,默认将key作为列表的元素)。
tuple:将一个可迭代对象转化成元祖(如果是字典,默认将key作为元祖的元素)。
1 l = list((1,2,3))
2 print(l)
3
4 l = list({1,2,3})
5 print(l)
6
7 l = list({'k1':1,'k2':2})
8 print(l)
9
10 tu = tuple((1,2,3))
11 print(tu)
12
13 tu = tuple([1,2,3])
14 print(tu)
15
16 tu = tuple({'k1':1,'k2':2})
17 print(tu)
View Code
序列——字符串相关的:str,format,bytes,bytearry,memoryview,ord,chr,ascii,repr
str:将数据转化成字符串。
format:与具体数据相关,用于计算各种小数,精算等。
1 #字符串可以提供的参数,指定对齐方式,<是左对齐, >是右对齐,^是居中对齐
2 print(format('test', '<20'))
3 print(format('test', '>20'))
4 print(format('test', '^20'))
5
6 #整形数值可以提供的参数有 'b' 'c' 'd' 'o' 'x' 'X' 'n' None
7 >>> format(3,'b') #转换成二进制
8 '11'
9 >>> format(97,'c') #转换unicode成字符
10 'a'
11 >>> format(11,'d') #转换成10进制
12 '11'
13 >>> format(11,'o') #转换成8进制
14 '13'
15 >>> format(11,'x') #转换成16进制 小写字母表示
16 'b'
17 >>> format(11,'X') #转换成16进制 大写字母表示
18 'B'
19 >>> format(11,'n') #和d一样
20 '11'
21 >>> format(11) #默认和d一样
22 '11'
23
24 #浮点数可以提供的参数有 'e' 'E' 'f' 'F' 'g' 'G' 'n' '%' None
25 >>> format(314159267,'e') #科学计数法,默认保留6位小数
26 '3.141593e+08'
27 >>> format(314159267,'0.2e') #科学计数法,指定保留2位小数
28 '3.14e+08'
29 >>> format(314159267,'0.2E') #科学计数法,指定保留2位小数,采用大写E表示
30 '3.14E+08'
31 >>> format(314159267,'f') #小数点计数法,默认保留6位小数
32 '314159267.000000'
33 >>> format(3.14159267000,'f') #小数点计数法,默认保留6位小数
34 '3.141593'
35 >>> format(3.14159267000,'0.8f') #小数点计数法,指定保留8位小数
36 '3.14159267'
37 >>> format(3.14159267000,'0.10f') #小数点计数法,指定保留10位小数
38 '3.1415926700'
39 >>> format(3.14e+1000000,'F') #小数点计数法,无穷大转换成大小字母
40 'INF'
41
42 #g的格式化比较特殊,假设p为格式中指定的保留小数位数,先尝试采用科学计数法格式化,得到幂指数exp,如果-4<=exp<p,则采用小数计数法,并保留p-1-exp位小数,否则按小数计数法计数,并按p-1保留小数位数
43 >>> format(0.00003141566,'.1g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留0位小数点
44 '3e-05'
45 >>> format(0.00003141566,'.2g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留1位小数点
46 '3.1e-05'
47 >>> format(0.00003141566,'.3g') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留2位小数点
48 '3.14e-05'
49 >>> format(0.00003141566,'.3G') #p=1,exp=-5 ==》 -4<=exp<p不成立,按科学计数法计数,保留0位小数点,E使用大写
50 '3.14E-05'
51 >>> format(3.1415926777,'.1g') #p=1,exp=0 ==》 -4<=exp<p成立,按小数计数法计数,保留0位小数点
52 '3'
53 >>> format(3.1415926777,'.2g') #p=1,exp=0 ==》 -4<=exp<p成立,按小数计数法计数,保留1位小数点
54 '3.1'
55 >>> format(3.1415926777,'.3g') #p=1,exp=0 ==》 -4<=exp<p成立,按小数计数法计数,保留2位小数点
56 '3.14'
57 >>> format(0.00003141566,'.1n') #和g相同
58 '3e-05'
59 >>> format(0.00003141566,'.3n') #和g相同
60 '3.14e-05'
61 >>> format(0.00003141566) #和g相同
62 '3.141566e-05'
View Code
bytes:用于不同编码之间的转化。
1 # s = '你好'
2 # bs = s.encode('utf-8')
3 # print(bs)
4 # s1 = bs.decode('utf-8')
5 # print(s1)
6 # bs = bytes(s,encoding='utf-8')
7 # print(bs)
8 # b = '你好'.encode('gbk')
9 # b1 = b.decode('gbk')
10 # print(b1.encode('utf-8'))
View Code
bytearry:返回一个新字节数组。这个数组里的元素是可变的,并且每个元素的值范围: 0 <= x < 256。
memoryview
1 ret = bytearray('alex',encoding='utf-8')
2 print(id(ret))
3 print(ret[0])
4 ret[0] = 65
5 print(ret)
6 print(id(ret))
bytearray
1 ret = memoryview(bytes('你好',encoding='utf-8'))
2 print(len(ret))
3 print(bytes(ret[:3]).decode('utf-8'))
4 print(bytes(ret[3:]).decode('utf-8'))
memoryview
ord:输入字符找该字符编码的位置
chr:输入位置数字找出其对应的字符
ascii:是ascii码中的返回该值,不是就返回/u...
1 # ord 输入字符找该字符编码的位置
2 # print(ord('a'))
3 # print(ord('中'))
4
5 # chr 输入位置数字找出其对应的字符
6 # print(chr(97))
7 # print(chr(20013))
8
9 # 是ascii码中的返回该值,不是就返回/u...
10 # print(ascii('a'))
11 # print(ascii('中'))
View Code
repr:返回一个对象的string形式(原形毕露)。
1 # %r 原封不动的写出来
2 # name = 'taibai'
3 # print('我叫%r'%name)
4
5 # repr 原形毕露
6 print(repr('{"name":"alex"}'))
7 print('{"name":"alex"}')
View Code
序列:reversed,slice
reversed:将一个序列翻转,并返回此翻转序列的迭代器。
slice:构造一个切片对象,用于列表的切片。
1 l = (1,2,23,213,5612,342,43)
2 print(l)
3 print(list(reversed(l)))
reversed
1 l = (1,2,23,213,5612,342,43)
2 sli = slice(1,5,2)
3 print(l[sli])
slice
数据集合——字典和集合:dict,set,frozenset
dict:创建一个字典。
set:创建一个集合。
frozenset:返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。
数据集合:len,sorted,enumerate,all,any,zip,filter,map
len:返回一个对象中元素的个数。
sorted:对所有可迭代的对象进行排序操作。
1 L = [('a', 1), ('c', 3), ('d', 4),('b', 2), ]
2 sorted(L, key=lambda x:x[1]) # 利用key
3 [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
4
5
6 students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
7 sorted(students, key=lambda s: s[2]) # 按年龄排序
8 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
9
10 sorted(students, key=lambda s: s[2], reverse=True) # 按降序
11 [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
View Code
enumerate:枚举,返回一个枚举对象。
1 print(enumerate([1,2,3]))
2 for i in enumerate([1,2,3]):
3 print(i)
4 for i in enumerate([1,2,3],100):
5 print(i)
View Code
all:可迭代对象中,全都是True才是True
any:可迭代对象中,有一个True 就是True
1 # all 可迭代对象中,全都是True才是True
2 # any 可迭代对象中,有一个True 就是True
3 # print(all([1,2,True,0]))
4 # print(any([1,'',0]))
View Code
zip:函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同。
1 l1 = [1,2,3,]
2 l2 = ['a','b','c',5]
3 l3 = ('*','**',(1,2,3))
4 for i in zip(l1,l2,l3):
5 print(i)
View Code
filter:过滤·。
1 #filter 过滤 通过你的函数,过滤一个可迭代对象,返回的是True
2 #类似于[i for i in range(10) if i > 3]
3 # def func(x):return x%2 == 0
4 # ret = filter(func,[1,2,3,4,5,6,7])
5 # print(ret)
6 # for i in ret:
7 # print(i)
View Code
map:会根据提供的函数对指定序列做映射。
1 >>>def square(x) : # 计算平方数
2 ... return x ** 2
3 ...
4 >>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
5 [1, 4, 9, 16, 25]
6 >>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数
7 [1, 4, 9, 16, 25]
8
9 # 提供了两个列表,对相同位置的列表数据进行相加
10 >>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
11 [3, 7, 11, 15, 19]
View Code
4、迭代器/生成器相关
range:函数可创建一个整数对象,一般用在 for 循环中。
next:内部实际使用了__next__方法,返回迭代器的下一个项目。
1 # 首先获得Iterator对象:
2 it = iter([1, 2, 3, 4, 5])
3 # 循环:
4 while True:
5 try:
6 # 获得下一个值:
7 x = next(it)
8 print(x)
9 except StopIteration:
10 # 遇到StopIteration就退出循环
11 break
View Code
iter:函数用来生成迭代器。
1 from collections import Iterable
2 from collections import Iterator
3 l = [1,2,3]
4 print(isinstance(l,Iterable)) # True
5 print(isinstance(l,Iterator)) # False
6
7 l1 = iter(l)
8 print(isinstance(l1,Iterable)) # True
9 print(isinstance(l1,Iterator)) # True
View Code
5、反射相关
6、面向对象相关
type(o) 返回变量o的数据类型
# 标红的必会
# 标黄的 是能够节省你的代码
# 重点:min max sorted filter map
二、匿名函数
匿名函数:为了解决那些功能很简单的需求而设计的一句话函数
1 #这段代码
2 def calc(n):
3 return n**n
4 print(calc(10))
5
6 #换成匿名函数
7 calc = lambda n:n**n
8 print(calc(10))
关于匿名函数格式的说明:
1 函数名 = lambda 参数 :返回值
2
3 # 参数可以有多个,用逗号隔开
4 # 匿名函数不管逻辑多复杂,只能写一行,且逻辑执行结束后的内容就是返回值
5 # 返回值和正常的函数一样可以是任意数据类型
匿名函数并不是真的不能有名字。
匿名函数的调用和正常的调用没有什么区别。
函数名(参数)
例1:练一练
1 # 函数
2 def add(a,b):
3 return a+b
4
5 # 换成匿名函数
6 add = lambda a,b:a+b
7 print(add(1,2))
例2:
1 # 列表推导式
2 [i**2 for i in range(10)]
3
4 # 函数
5 def func(num):
6 return num ** 2
7 for i in map(func,range(10)):print(i)
8
9 # 匿名函数
10 for i in map(lambda num:num ** 2 ,range(10)):print(i)
例3:
1 # 函数
2 def func(num):
3 return num%2
4 print(min(-2,3,-4,key=func))
5
6 # 匿名行数
7 print(min(-2,3,-4,key=lambda num:num % 2))
例4:
1 l=[3,2,100,999,213,1111,31121,333]
2 print(max(l))
3
4 dic={'k1':10,'k2':100,'k3':30}
5
6
7 print(max(dic))
8 print(dic[max(dic,key=lambda k:dic[k])])
例5:
1 res = map(lambda x:x**2,[1,5,7,4,8])
2 for i in res:
3 print(i)
4
5 # 输出:
6 '''
7 1
8 25
9 49
10 16
11 64
12 '''
例6:
1 res = filter(lambda x:x>10,[5,8,11,9,15])
2 for i in res:
3 print(i)
4
5 # 输出:
6 '''
7 11
8 15
9 '''
面试题:
1 # 1.下面程序的输出结果是:
2 d = lambda p:p*2
3 t = lambda p:p*3
4 x = 2
5 x = d(x)
6 x = t(x)
7 x = d(x)
8 print(x)
9
10 # 2.现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]
11 def func(t):
12 return {t[0]:t[1]}
13 ret = map(func,zip((('a'),('b')),(('c'),('d'))))
14 print(list(ret))
15
16 ret = map(lambda t:{t[0]:t[1]},zip((('a'),('b')),(('c'),('d'))))
17 print(list(ret))
18
19 #答案一
20 test = lambda t1,t2 :[{i:j} for i,j in zip(t1,t2)]
21 print(test(t1,t2))
22 #答案二
23 print(list(map(lambda t:{t[0]:t[1]},zip(t1,t2))))
24 #还可以这样写
25 print([{i:j} for i,j in zip(t1,t2)])
26
27 # 3.以下代码的输出是什么?请给出答案并解释。
28 def multipliers():
29 return [lambda x:i*x for i in range(4)]
30 print([m(2) for m in multipliers()])
31 # 请修改multipliers的定义来产生期望的结果。
32 def multipliers():
33 lst = []
34 for i in range(4):
35 lst.append(lambda x:i*x)
36 return lst
37 print([m(2) for m in multipliers()])
38
39 def multipliers():
40 return [lambda x:i*x for i in range(4)]
41 print([m(2) for m in multipliers()])
42
43 g = (lambda x:i*x for i in range(4))
44 print([m(2) for m in g])
三、本章小结
其他:input,print,type,hash,open,import,dir
str类型代码执行:eval,exec
数字:bool,int,float,abs,divmod,min,max,sum,round,pow
序列——列表和元组相关的:list和tuple
序列——字符串相关的:str,bytes,repr
序列:reversed,slice
数据集合——字典和集合:dict,set,frozenset
数据集合:len,sorted,enumerate,zip,filter,map
1 # map filter sorted max min zip
2 def func(n):
3 if n%2 == 0:
4 return True
5 ret = filter(func,range(20))
6 ret = filter(lambda n: True if n%2 == 0 else False ,range(20))
7 ret = filter(lambda n: n%2 == 0 ,range(20))
参考文档:
https://docs.python.org/3/library/functions.html#object