对于 Python 3,该方法为 zip_longest
:
from itertools import zip_longest
对于Python 2, 方法是 izip_longest
:
from itertools import izip_longest
如果您不知道脚本运行的是哪个python版本,可以使用以下技巧:
try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
# now this works in both python 2 and 3
print(list(zip_longest([1,2,3],[4,5])))