我在Python的itertools中找不到imap() [英] I can't find imap() in itertools in Python

查看:486
本文介绍了我在Python的itertools中找不到imap()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要使用itertools.imap()解决.但是,在将itertools导入IDLE shell中并调用了itertools.imap()之后,IDLE shell告诉我itertools没有属性imap.怎么了?

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?

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

推荐答案

itertools.imap()在Python 2中,但在Python 3中不是.

itertools.imap() is in Python 2, but not in Python 3.

实际上,该函数在Python 3中仅移至map函数,如果要使用旧的Python 2映射,则必须使用list(map()).

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()).

这篇关于我在Python的itertools中找不到imap()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆