My IDLE does not recognize itertools.izip() as a f

2020-07-06 05:52发布

问题:

>>> 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'

回答1:

In Python 3, there is no izip function in the itertools module because the builtin zip function (which doesn't require any imports to access) now behaves like itertools.izip did in Python 2. So, to make your code work, just use zip instead of itertools.izip.

You also mentioned an issue with string.maketrans. That's another function that is no longer in a module in Python 3. It's now a method of the str class: str.maketrans. Note however that its behavior is a bit different than string.maketrans in Python 2, as the translate method on strings takes different arguments (a dictionary instead of a 256-character string).

It sounds like you may be following a guide written for Python 2, but using Python 3 to run your code. This can be confusing, as there were signficant changes between the major versions of the language. You should try to find a guide that targets Python 3 instead. I don't recommend using Python 2 for your coding unless you really must follow your current guide.