猿问

TypeError:“枚举”类型的对象没有 len()

这是我的代码:


def twosum(a, t):

    a = enumerate(a)

    f, l = 0, len(a)-1

    while(f <= l):

        if (a[f][1] + a[l][1] == t):

            return [a[f][0], a[l][0]]

        else:

            f += 1

            l -= 1

    return


print(twosum([2, 7, 11, 15], 9)) 

我得到错误:


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

我认为我们可以将枚举对象视为列表。我在网上找到了将枚举对象视为列表的解决方案。谁能解释我为什么会收到这个错误?


慕田峪4524236
浏览 281回答 3
3回答

炎炎设计

TypeError:&nbsp;object&nbsp;of&nbsp;type&nbsp;'enumerate'&nbsp;has&nbsp;no&nbsp;len()该len()功能要求对象具有或实现该__len__功能。object.__len__(self)调用以实现内置函数len()。应该返回对象的长度,整数 >= 0。不幸的是,enumerate返回一个没有的枚举对象__len__:>>> a = enumerate([1,2,3])>>> a<enumerate object at 0x10e496be0>>>> dir(a)['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',&nbsp;'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',&nbsp;&nbsp;'__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__',&nbsp;'__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',&nbsp;&nbsp;'__sizeof__', '__str__', '__subclasshook__']与list支持的 's不同len():>>> a = [1,2,3]>>> dir(a)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',&nbsp;&nbsp;...&nbsp;'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',&nbsp;&nbsp;&nbsp;'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__',&nbsp;&nbsp;...&nbsp;'index', 'insert', 'pop', 'remove', 'reverse', 'sort']您还可以注意到enumerate 对象也没有允许您使用like for s__getitem__访问项目的方法。这就是为什么您在回答中说“它甚至不可下标”。obj[index]list'我认为我们可以将枚举对象视为列表。不,不是。枚举对象的行为更像一个迭代器,这是 Python 表示可能是无限的“数据流”的方式。您可以通过调用该next()方法来访问数据,直到引发异常 ( StopIteration)。重复调用迭代器的__next__()方法(或将其传递给内置函数next())会返回流中的连续项。当没有更多数据可用时,StopIteration会引发异常。>>> a = enumerate([1,2,3])>>> next(a)(0, 1)>>> next(a)(1, 2)>>> next(a)(2, 3)>>> next(a)Traceback (most recent call last):&nbsp; File "<stdin>", line 1, in <module>StopIteration我认为您会认为它们就像list's 一样,因为您也可以将它们放入像常规一样的循环结构中list并遍历每个元素:>>> a = enumerate([1,2,3])>>> for i in a:...&nbsp; &nbsp;print(i)...&nbsp;(0, 1)(1, 2)(2, 3)在这种情况下,对于每次迭代,枚举对象都会提供一个元组,其中包含下一个元素的索引和元素本身。循环的for工作和结束方式与使用enumerate objectnext()的方法时相同。如enumerate()文档中所示,list如果您需要类似列表的内容,您可以简单地将其转换为 a:>>> a = list(enumerate([1,2,3]))>>> a[(0, 1), (1, 2), (2, 3)]

杨__羊羊

enumerate只是为可迭代添加一个计数器,它不是可迭代的。枚举对象可以转换为列表,然后可以使用。至于您的问题,可以使用非枚举解决方案def twosum(a, t):&nbsp; &nbsp; f, l = 0, len(a) - 1&nbsp; &nbsp; while f <= l:&nbsp; &nbsp; &nbsp; &nbsp; if a[f] + a[l] == t:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [a.index(a[f]), a.index(a[l])]&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l -= 1twosum([2, 7, 11, 15], 17)这将返回 [0, 3]。对于枚举解决方案,def twosum(a, t):&nbsp; &nbsp; f, l = 0, len(a)-1&nbsp; &nbsp; a = list(enumerate(a))&nbsp; &nbsp; while(f <= l):&nbsp; &nbsp; &nbsp; &nbsp; if (a[f][1] + a[l][1] == t):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [a[f][0], a[l][0]]&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l -= 1twosum([2, 7, 11, 15], 17)这也返回 [0, 3]

潇湘沐

这个解决方案奏效了。我必须先将枚举对象转换为列表。否则它甚至不能下标。def twosum(a, t):&nbsp; &nbsp; a = enumerate(a)&nbsp; &nbsp; a = sorted(a, key=lambda x:x[1])&nbsp; &nbsp; f, l = 0, len(a)-1&nbsp; &nbsp; while(f < l):&nbsp; &nbsp; &nbsp; &nbsp; if (a[f][1] + a[l][1] == t):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [a[f][0], a[l][0]]&nbsp; &nbsp; &nbsp; &nbsp; elif (a[f][1] + a[l][1] < t):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f += 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l -= 1
随时随地看视频慕课网APP

相关分类

Python
我要回答