Python 内建函数 - map(function, iterable, ...)

Manual

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

直译

返回一个迭代器, 其将function应用到iterable的每个项,并生成结果。如果传入额外的iterable参数,函数必须获取那些参数并平行应用到所有iterable的项。对于多重迭代,当最短的迭代完时迭代器终止。对于已经排列为参数数组的情况,详见itertools.starmap()

实例

# 与filter()同样的输入,观察结果
>>> list(map(lambda x:x%2, range(10)))
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

# map后输出长度保持不变,无有效值则返回None
>>> def eventest(x):
    if x % 2 == 0:
        return x
>>> list(map(eventest, range(10)))
[0, None, 2, None, 4, None, 6, None, 8, None]

# 多参数情况下,输出长度由最短输入长度决定
>>> def foobar(x, y, z):
    return (x + y) * z /2
>>> a = [2, 5, 6, 9]
>>> b = [4, 10, 12, 18, 22]
>>> c = [7, 8, 10, 11, 13, 14]
>>> list(map(foobar, a, b, c,))
[21.0, 60.0, 90.0, 148.5]

拓展阅读

itertools.starmap()
filter()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值