1 2 3 4 5 |
from random import randint allNums = [] for eachNum in range(9): allNums.append(randint(1, 99)) print filter(lambda n: n%2, allNums) |
filter就是把前边的函数应用到没个元素上,然后true的留下,false的滚
1 2 3 4 5 6 7 8 9 10 |
>>> map((lambda x: x+2), [0, 1, 2, 3, 4, 5]) [2, 3, 4, 5, 6, 7] >>> >>> map(lambda x: x**2, range(6)) [0, 1, 4, 9, 16, 25] >>> [x+2 for x in range(6)] [2, 3, 4, 5, 6, 7] >>> >>>[x**2 for x in range(6)] [0, 1, 4, 9, 16, 25] |
map的作用是将函数应用到没个元素,得到一个由新元素组成的新的数组
1 2 3 4 5 |
>>> print 'the total is:', reduce((lambda x,y: x+y), range(5)) the total is: 10 给出了上面的输入,reduce()函数运行了如下的算术操作。 ((((0 + 1) + 2) + 3) + 4) => 10 |
reduce的效果就在上边,前来两个得到一个新的,然后新的元素和第三个进行处理…..
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021