实际上,使用多个可迭代对象作为参数的map
将会对这些可迭代对象进行zip
操作,然后使用来自zip
的元组作为变长参数调用函数。因此,您可以使用itertools.starmap
和zip
来获得相同的行为:
>>> a = [10, 20]
>>> b = [1, 2, 3]
>>> f = lambda x, y: x
>>> list(map(f, a, b))
[10, 20]
>>> from itertools import starmap
>>> list(starmap(f, zip(a, b)))
[10, 20]
如果您想要的行为是将 zip
替换为 itertools.zip_longest
,则可以实现:
>>> from itertools import starmap, zip_longest
>>> list(starmap(f, zip_longest(a, b)))
[10, 20, None]
itertools
的这两个函数在Python 2中也存在,只是第二个函数被命名为izip_longest
。你可以通过import ... as ...
来解决这个问题。
from past.builtin import map
:-) - Nishantlen(list_1) >= len(list_2) and all(list_1)
似乎是解决你所展示的特定问题的更好方法,而不必担心向后兼容版本的map
或任何其他替代方案(如itertools.zip_longest
)。 - Blckknghtall(map(compare_items, found, expected))
,似乎这种行为是真正需要的。 - Nishant