"map"类型的对象在Python 3中没有len() [英] Object of type 'map' has no len() in Python 3

查看:500
本文介绍了"map"类型的对象在Python 3中没有len()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python 3有问题.我得到了Python 2.7代码,此刻我正尝试对其进行更新.我收到错误消息:

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:地图"类型的对象没有len()

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

在此部分:

str(len(seed_candidates))

在像这样初始化之前:

seed_candidates = map(modify_word, wordlist)

那么,有人可以向我解释我该怎么做吗?

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

(以前,此代码示例是错误的,因为它使用set而不是map.现在已更新.)

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

推荐答案

在Python 3中,map返回一个映射对象,而不是list:

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

这篇关于"map"类型的对象在Python 3中没有len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆