Object of type 'map' has no len() in Python 3

这一生的挚爱 提交于 2019-11-27 04:35:45

问题


I have a problem with Python 3. I got Python 2.7 code and at the moment I am trying to update it. I get the error:

TypeError: object of type 'map' has no len()

at this part:

str(len(seed_candidates))

Before I initialized it like this:

seed_candidates = map(modify_word, wordlist)

So, can someone explain me what I have to do?

(EDIT: Previously this code example was wrong because it used set instead of map. It has been updated now.)


回答1:


In Python 3, map returns a map object not a 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()

You can convert it into a list then get the length from there:

>>> print(len(list(L)))
10


来源:https://stackoverflow.com/questions/36982858/object-of-type-map-has-no-len-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!