【问题标题】:My IDLE does not recognize itertools.izip() as a function我的 IDLE 无法将 itertools.izip() 识别为函数
【发布时间】:2015-11-22 13:31:08
【问题描述】:
>>> 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?在 Python 3 中,内置函数 zip 的行为类似于 Python 2 中的 itertools.izip
  • Blckknght 是对的。如果你不知道如何找到 python 版本,请看这里:stackoverflow.com/questions/9079036/…
  • @Blckknght 嗨,Blckknght,我使用的是 3.4.3 版。您也可以在 shell 窗口的顶部栏中看到它。我对 string.maketrans(...) 有同样的问题。见:
  • >>> trans = string.makestrans('ae','bx') Traceback(最近一次调用最后):文件“”,第 1 行,在 中= string.makestrans('ae','bx') AttributeError: 'module' object has no attribute 'makestrans'

标签: python itertools


【解决方案1】:

在 Python 3 中,itertools 模块中没有 izip 函数,因为内置的 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 进行编码,除非您确实必须遵循当前指南。

【讨论】:

  • 感谢您的回复。事实上,我正在关注一个关于滑块的在线课程视频,该视频一定是使用 Python 2 录制的。
猜你喜欢
  • 2020-11-01
  • 1970-01-01
  • 2014-12-10
  • 2021-10-01
  • 2021-07-23
  • 2020-01-27
  • 1970-01-01
  • 2011-07-10
  • 2017-06-04
相关资源
最近更新 更多