1. 映射函数
- items=[1,2,3,4,5]
- def inc(x):
- return x+1
- list(map(inc,items))
- [2, 3, 4, 5, 6]
- >>> a = [i+1 for i in range(3)]
- >>> list(map((lambda x:x+5),a))
- [5, 6, 7]
2. 过滤函数
- >>> list(filter((lambda x:x>1),a))
- [2]
3. 规约函数: 将列表所有元素依次计算后返回唯一结果
累加:
- >>> from functools import reduce
- >>> reduce((lambda x,y:x+y),a)
- 45
累乘
- >>> arr = [i+1 for i in range(4)]
- >>> reduce((lambda x,y:x*y),arr)
- 24
4.lamdba 函数: 匿名函数, 其他列子里有不列举
5. 列表生成式
- >>> a = [i+1 for i in range(3)]
- >>> a
- [1, 2, 3]
练习: 列表 [1,2,3,4,5] 中过滤奇数.
- >>> arr = list(filter((lambda x:x%2==0),[i+1 for i in range(5)]))
- >>> arr
- [2, 4]
练习: 通过两个列表构建字典.
- >>> a = [i+1 for i in range(3)]
- >>> b = ['a','b','c']
- >>> c = {i:j for i in a for j in b}
- >>> c
- {0: 'c', 1: 'c', 2: 'c'}
posted on 2018-05-10 23:43 灰了个灰 阅读(...) 评论(...) 编辑 收藏
来源: https://www.cnblogs.com/fine-6/p/9022433.html