>>> 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中,izip模块中没有itertools函数,因为内置的zip函数(不需要任何导入即可访问)现在的行为就像python 2中的itertools.izip一样。因此,要使您的代码正常工作,只需使用zip即可的itertools.izip

您还提到了string.maketrans的问题。那是Python 3中一个模块中不再存在的另一个函数,它现在是str类的一种方法:str.maketrans。但是请注意,它的行为与Python 2中的string.maketrans有点不同,因为字符串上的translate方法采用不同的参数(字典而不是256个字符的字符串)。

听起来您可能正在遵循为Python 2编写的指南,但使用Python 3运行代码。这可能会造成混淆,因为该语言的主要版本之间存在重大变化。您应该尝试查找针对Python 3的指南。我不建议您使用Python 2进行编码,除非您确实必须遵循当前指南。

10-02 07:35
查看更多