Object of type 'map' has no len() in Pytho

2020-01-29 08:39发布

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条回答
仙女界的扛把子
2楼-- · 2020-01-29 08:49

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
查看更多
登录 后发表回答