[英]line of code using zip_longest in itertools module for Python
[英]How to make zip_longest available in itertools using Python 2.7
对于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])))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.