蟒蛇的map
可以接受多个可迭代对象,当可调用对象可以接受相同数量的输入参数时使用。如果输入可迭代的长度相同,则其行为类似于传递压缩参数的列表理解,例如:
>>> iterables = 'spam', 'eggs'
>>> map(max, *iterables)
['s', 'p', 'g', 's']
>>> [max(*a) for a in zip(*iterables)]
['s', 'p', 'g', 's']
当输入参数的长度不同时,它会变得很奇怪 - Python 2 (docs) 垫与None
,但是 Python 3 (docs) 截断为最短迭代。
>>> map(max, 'spam', 'potato') # 2.x
['s', 'p', 't', 'm', 't', 'o']
>>> list(map(max, 'spam', 'potato')) # 3.x
['s', 'p', 't', 'm']
为什么存在此功能,需要或有用的典型案例是什么?我对功能风格了解不多,我是否会错过一些强大的优势map
与多个参数相关的? 3.x 中 API 更改的理由是什么?