Python-map&for&lambda函数-列表解析

121 阅读1分钟

函数ord:返回一个单个字符的ASCII整数编码 chr函数是它的逆过程

>>> res=[]
>>> for x in 'spam':
	res.append(ord(x))

	
>>> res
[115, 112, 97, 109]
>>> res=list(map(ord,'spam') )
>>> res
[115, 112, 97, 109]
>>>
>>> list(map((lambda x:x**2),filter((lambda x:x%2==0),range(10))))
[0, 4, 16, 36, 64]

>>> res=[x+y for x in[0,1,2] for y in [100,200,300]]
>>> res
[100, 200, 300, 101, 201, 301, 102, 202, 302]

>>> [(x,y)for x in range(5) if x%2==0 for y in range(5) if y%2 ==1]
[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]