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

tuples = (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))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值