我有一个问题,我想用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'
Run Code Online (Sandbox Code Playgroud)
mar*_*kzz 32
itertools.imap()
是在Python 2中,但在Python 3中没有.
实际上,该功能只是移动到map
Python 3中的功能,如果你想使用旧的Python 2地图,你必须使用list(map())
.
daw*_*awg 15
如果你想要一些适用于Python 3和Python 2的东西,你可以这样做:
try:
from itertools import imap
except ImportError:
# Python 3...
imap=map
Run Code Online (Sandbox Code Playgroud)