我在 Python 的 itertools 中找不到 imap()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30271712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't find imap() in itertools in Python
提问by
I have a problem that I want to solve with itertools.imap(). However, after I imported itertools in IDLE shell and called itertools.imap(), the IDLE shell told me that itertools doesn't have attribute imap. What's going wrong?
我有一个问题想用 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'
采纳答案by markzz
itertools.imap()
is in Python 2, but not in Python 3.
itertools.imap()
在 Python 2 中,但不在 Python 3 中。
Actually, that function was moved to just the map
function in Python 3 and if you want to use the old Python 2 map, you must use list(map())
.
实际上,该函数仅移至map
Python 3 中的函数,如果您想使用旧的 Python 2 映射,则必须使用list(map())
.
回答by Alik
回答by dawg
If you want something that works in both Python 3 and Python 2, you can do something like:
如果您想要在 Python 3 和 Python 2 中都能使用的东西,您可以执行以下操作:
try:
from itertools import imap
except ImportError:
# Python 3...
imap=map
回答by Inversus
How about this?
这个怎么样?
imap = lambda *args, **kwargs: list(map(*args, **kwargs))
In fact!! :)
实际上!!:)
import itertools
itertools.imap = lambda *args, **kwargs: list(map(*args, **kwargs))
回答by hobs
I like the python-future
idomsfor universal Python 2/3 code, like this:
我喜欢通用 Python 2/3 代码的python-future
idom,如下所示:
# Works in both Python 2 and 3:
from builtins import map
You then have to refactor your code to use map
everywhere you were using imap
before:
然后,您必须重构您的代码以map
在您imap
之前使用的任何地方使用:
myiter = map(func, myoldlist)
# `myiter` now has the correct type and is interchangeable with `imap`
assert isinstance(myiter, iter)
You do need to install future for this to work on both 2 and 3:
您确实需要安装 future 才能在 2 和 3 上运行:
pip install future
回答by user2757572
You can use the 2to3 script (https://docs.python.org/2/library/2to3.html) which is part of every Python installation to translate your program or whole projects from Python 2 to Python 3.
您可以使用 2to3 脚本 ( https://docs.python.org/2/library/2to3.html),它是每个 Python 安装的一部分,将您的程序或整个项目从 Python 2 转换为 Python 3。
python <path_to_python_installation>\Tools\scriptsto3.py -w <your_file>.py
(-w option writes modifications to file, a backup is stored)
(-w 选项将修改写入文件,存储备份)