笔记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))