>>> itertools.izip('ABCD', 'xy')
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
itertools.izip('ABCD', 'xy')
AttributeError: 'module' object has no attribute 'izip'
最佳答案
在 Python 3 中,itertools
模块中没有 izip
函数,因为内置的 zip
函数(不需要任何导入访问)现在的行为就像 itertools.izip
在 Python 2 中所做的那样。因此,要使您的代码正常工作,只需使用 zip
而不是 itertools.izip
.
您还提到了 string.maketrans
的问题。这是另一个不再存在于 Python 3 模块中的函数。它现在是 str
类的一个方法:str.maketrans
。但是请注意,它的行为与 Python 2 中的 string.maketrans
有点不同,因为字符串上的 translate
方法采用不同的参数(字典而不是 256 个字符的字符串).
听起来您可能正在遵循为 Python 2 编写的指南,但使用 Python 3 来运行您的代码。这可能会造成混淆,因为该语言的主要版本之间存在重大变化。您应该尝试找到针对 Python 3 的指南。我不建议您使用 Python 2 进行编码,除非您确实必须遵循当前的指南。
关于python - 我的 IDLE 无法将 itertools.izip() 识别为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32261698/