【发布时间】:2017-06-10 21:11:10
【问题描述】:
尝试在 Windows 10 上运行的 Python Jupyter 2.7 nb 上导入此函数时,我收到此错误:
我相信我过去没有遇到过问题,因为我使用的是 Python 3。所以我想知道是否只是它在 Python 2 中不可用,或者是否有办法让它工作。
【问题讨论】:
标签: python python-2.7 itertools
尝试在 Windows 10 上运行的 Python Jupyter 2.7 nb 上导入此函数时,我收到此错误:
我相信我过去没有遇到过问题,因为我使用的是 Python 3。所以我想知道是否只是它在 Python 2 中不可用,或者是否有办法让它工作。
【问题讨论】:
标签: python python-2.7 itertools
对于 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])))
【讨论】: