问题
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