最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

笔记13-filter,map,reduce内置类的使用

互联网 admin 18浏览 0评论

笔记13-filter,map,reduce内置类的使用

# python2 filter是内置函数  python3 filter 是内置类
# filter两个参数,函数和可迭代对象
# filter对可迭代对象进行过滤,过滤的结果是filter对象(依然是可迭代对象)
ages = [12, 23, 30, 17, 16, 22, 19]
agesFilter = filter(lambda ele: ele > 18, ages)adult = list(agesFilter)
print(adult)# map内置类的使用
lists = [1, 2, 3, 4, 5, 6]
i = map(lambda element: element + 3, lists)
print(list(i))# reduce类的使用
from functools import reducetuples = (1, 2, 3, 4, 5, 6)
tuplesReduce = reduce(lambda x, y: x + y, tuples)
print(tuplesReduce)listDicts = [{"name": "胡勇", "age": 21, "height": 180},{"name": "卢雯婷", "age": 18, "height": 158},{"name": "喵喵", "age": 2, "height": 10},]
print(reduce(lambda x, y: x + y['age'], listDicts, 0))

笔记13-filter,map,reduce内置类的使用

# python2 filter是内置函数  python3 filter 是内置类
# filter两个参数,函数和可迭代对象
# filter对可迭代对象进行过滤,过滤的结果是filter对象(依然是可迭代对象)
ages = [12, 23, 30, 17, 16, 22, 19]
agesFilter = filter(lambda ele: ele > 18, ages)adult = list(agesFilter)
print(adult)# map内置类的使用
lists = [1, 2, 3, 4, 5, 6]
i = map(lambda element: element + 3, lists)
print(list(i))# reduce类的使用
from functools import reducetuples = (1, 2, 3, 4, 5, 6)
tuplesReduce = reduce(lambda x, y: x + y, tuples)
print(tuplesReduce)listDicts = [{"name": "胡勇", "age": 21, "height": 180},{"name": "卢雯婷", "age": 18, "height": 158},{"name": "喵喵", "age": 2, "height": 10},]
print(reduce(lambda x, y: x + y['age'], listDicts, 0))
发布评论

评论列表 (0)

  1. 暂无评论