我对Python 3有问题。我有Python 2.7代码,此刻我正在尝试对其进行更新。我得到错误:
TypeError:“地图”类型的对象没有len()
在这一部分:
str(len(seed_candidates))
在这样初始化之前:
seed_candidates = map(modify_word, wordlist)
那么,有人可以解释我该怎么做吗?
(编辑:以前,此代码示例是错误的,因为它使用set
代替map
。现在已更新。)
在Python 3中,map
返回的地图对象不是list
:
>>> L = map(str, range(10))
>>> print(L)
<map object at 0x101bda358>
>>> print(len(L))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'map' has no len()
您可以将其转换为列表,然后从那里获取长度:
>>> print(len(list(L)))
10
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句