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]