'map' 类型的对象在 Python 3 中没有 len()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36982858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:38:18  来源:igfitidea点击:

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

pythonpython-3.xvariable-length

提问by Sonius

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:

我的 Python 3 有问题。我得到了 Python 2.7 代码,目前我正在尝试更新它。我收到错误:

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

类型错误:“地图”类型的对象没有 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 setinstead of map. It has been updated now.)

(编辑:以前这个代码示例是错误的,因为它使用了set而不是map。现在已经更新了。)

回答by TerryA

In Python 3, mapreturns a map object not a list:

在 Python 3 中,map返回一个 map 对象而不是 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

回答by theshopen

While the accepted answer might work for the OP, there are some things to learn here, because sometimes you can't find the lengtheven with doing the OP's map(modify_word, wordlist)casted into list and checking the length with len(list(map(modify_word, wordlist))). You can't because sometimes the length is infinite.

虽然接受的答案可能适用于 OP,但这里有一些东西需要学习,因为有时即使将 OPmap(modify_word, wordlist)放入列表并使用len(list(map(modify_word, wordlist))). 你不能,因为有时长度是无限的

For example let's consider the following generator that lazy calculate all the naturals:

例如,让我们考虑以下惰性计算所有自然数的生成器:

def naturals():
    num = 0
    while True:
        yield num
        num +=1

And let's say I want to get the square of each of those, that is,

假设我想得到每个的平方,也就是说,

doubles = map(lambda x: x**2, naturals())

Note that this is a completely legit use of map function, and will work, and will allow you to use next() function on the doublesvariable:

请注意,这是对 map 函数的完全合法使用,并且会起作用,并且允许您对doubles变量使用 next() 函数:

>>> doubles = map(lambda x: x**2, naturals())
>>> next(doubles)
0
>>> next(doubles)
1
>>> next(doubles)
4
>>> next(doubles)
9
...

But, what if we try to cast it into a list? Obviously python can't know if we are trying to iterate through a never-ending iterator. So if we'll try to cast an instance of this mapObject to a list, python will try and keep tryingand will get stuck on an infinite loop.

但是,如果我们尝试将其转换为列表呢?显然 python 不知道我们是否正在尝试遍历一个永无止境的迭代器。因此,如果我们尝试将这个 mapObject 的一个实例转换为一个列表,python 将继续尝试并陷入无限循环。

So when you cast to list, you should first make sure you know your map object will indeed yield finite number of elements.

因此,当您转换为列表时,您应该首先确保您知道您的地图对象确实会产生有限数量的元素。