我在 Python 的 itertools 中找不到 imap()
I can't find imap() in itertools in Python
我有一个问题想用 itertools.imap() 解决。但是,在我在 IDLE shell 中导入 itertools 并调用 itertools.imap() 之后,IDLE shell 告诉我 itertools 没有属性 imap。怎么了?
>>> import itertools
>>> dir(itertools)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', '_grouper', '_tee', '_tee_dataobject', 'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']
>>> itertools.imap()
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
itertools.imap()
AttributeError: 'module' object has no attribute 'imap'
您正在使用 Python 3,因此 itertools
模块中没有 imap
功能。它被删除了,因为全局函数 map
现在 returns 迭代器。
itertools.imap()
在 Python 2 中,但不在 Python 3 中。
实际上,该功能已移至 Python 3 中的 map
功能,如果您想使用旧的 Python 2 映射,则必须使用 list(map())
.
如果你想要在 Python 3 和 Python 2 中都有效的东西,你可以这样做:
try:
from itertools import imap
except ImportError:
# Python 3...
imap=map
这个怎么样?
imap = lambda *args, **kwargs: list(map(*args, **kwargs))
事实上!! :)
import itertools
itertools.imap = lambda *args, **kwargs: list(map(*args, **kwargs))
我喜欢通用 Python 2/3 代码的 python-future
idoms,像这样:
# Works in both Python 2 and 3:
from builtins import map
然后您必须重构您的代码以在您之前使用 imap
的任何地方使用 map
:
myiter = map(func, myoldlist)
# `myiter` now has the correct type and is interchangeable with `imap`
assert isinstance(myiter, iter)
您需要安装 future 才能在 2 和 3 上运行:
pip install future
您可以使用每个 Python 安装中包含的 2to3 脚本 (https://docs.python.org/2/library/2to3.html) 将您的程序或整个项目从 Python 2 转换为 Python 3 .
python <path_to_python_installation>\Tools\scriptsto3.py -w <your_file>.py
(-w选项将修改写入文件,存储备份)
我有一个问题想用 itertools.imap() 解决。但是,在我在 IDLE shell 中导入 itertools 并调用 itertools.imap() 之后,IDLE shell 告诉我 itertools 没有属性 imap。怎么了?
>>> import itertools
>>> dir(itertools)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', '_grouper', '_tee', '_tee_dataobject', 'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']
>>> itertools.imap()
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
itertools.imap()
AttributeError: 'module' object has no attribute 'imap'
您正在使用 Python 3,因此 itertools
模块中没有 imap
功能。它被删除了,因为全局函数 map
现在 returns 迭代器。
itertools.imap()
在 Python 2 中,但不在 Python 3 中。
实际上,该功能已移至 Python 3 中的 map
功能,如果您想使用旧的 Python 2 映射,则必须使用 list(map())
.
如果你想要在 Python 3 和 Python 2 中都有效的东西,你可以这样做:
try:
from itertools import imap
except ImportError:
# Python 3...
imap=map
这个怎么样?
imap = lambda *args, **kwargs: list(map(*args, **kwargs))
事实上!! :)
import itertools
itertools.imap = lambda *args, **kwargs: list(map(*args, **kwargs))
我喜欢通用 Python 2/3 代码的 python-future
idoms,像这样:
# Works in both Python 2 and 3:
from builtins import map
然后您必须重构您的代码以在您之前使用 imap
的任何地方使用 map
:
myiter = map(func, myoldlist)
# `myiter` now has the correct type and is interchangeable with `imap`
assert isinstance(myiter, iter)
您需要安装 future 才能在 2 和 3 上运行:
pip install future
您可以使用每个 Python 安装中包含的 2to3 脚本 (https://docs.python.org/2/library/2to3.html) 将您的程序或整个项目从 Python 2 转换为 Python 3 .
python <path_to_python_installation>\Tools\scriptsto3.py -w <your_file>.py
(-w选项将修改写入文件,存储备份)