简体   繁体   English

Python - len function 没有按预期工作,并给我错误“TypeError: object of type 'int' has no len()”

[英]Python - len function not working as expected, and giving me the error "TypeError: object of type 'int' has no len()"

While studying linked lists from - https://composingprograms.com/pages/23-sequences.html#linked-lists在学习链表时 - https://composingprograms.com/pages/23-sequences.html#linked-lists

empty ='empty'
four = [1, [2, [3, [4, 'empty']]]]
x = [1,2];

def is_link(s):
    """s is a linked list if it is empty or a (first, rest) pair."""
    return s == empty or (len(s) == 2 and is_link(s[1]))

print(is_link(four))
print(is_link(x))

The program recognizes four as a linked list, But when i plug in x it returns an error instead of returning "False".该程序将四个识别为链表,但是当我插入 x 时,它返回错误而不是返回“False”。

If i change value of x to just [1] or [1,2,3] it returns as expected, but if i enter a normal list [1,2] with 2 values i run into this error.如果我将 x 的值更改为 [1] 或 [1,2,3] 它会按预期返回,但是如果我输入具有 2 个值的普通列表 [1,2] 我会遇到此错误。 .Why is this? 。为什么是这样?

The problem is in this part:问题出在这部分:

is_link(s[1])

When you pass x[1] to is_link method s is 2 (has type int) thus it doesnt have len() method.当您将 x[1] 传递给 is_link 方法时,s 为 2(具有 int 类型),因此它没有 len() 方法。 You need to check whether the argument in method is_link is list like that您需要检查方法 is_link 中的参数是否是这样的列表

return type(s) == list and (s == empty or (len(s) == 2 and is_link(s[1])))

Look at the executions, on your condition len(s) == 2 this only satisfies [1, 2] so it checks for the next one is_link(s[1]) which raises an error because it's a check length of integer in next iteration.查看执行情况,在您的条件下len(s) == 2这仅满足[1, 2]因此它检查下一个is_link(s[1])会引发错误,因为它的检查长度为 integer 在下一个迭代。

In [24]: print(is_link([1]))
[1]
False

In [25]: print(is_link([1, 2, 3]))
[1, 2, 3]
False

In [26]: print(is_link([1, 2]))
[1, 2]
2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [26], in <module>
----> 1 print(is_link([1, 2]))

Input In [20], in is_link(s)
      2 print(s)
      3 """s is a linked list if it is empty or a (first, rest) pair."""
----> 4 return s == empty or (len(s) == 2 and is_link(s[1]))

Input In [20], in is_link(s)
      2 print(s)
      3 """s is a linked list if it is empty or a (first, rest) pair."""
----> 4 return s == empty or (len(s) == 2 and is_link(s[1]))

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

When you try is_link([1,2]) , then, since the list is of length 2, it will try to evaluate is_link(s[1]) .当您尝试is_link([1,2])时,由于列表的长度为 2,它将尝试评估is_link(s[1]) Which in this case is is_link(2) .在这种情况下是is_link(2) Which then leads to len(2) which gives the error message, since 2 is not a list.然后导致len(2)给出错误消息,因为 2 不是列表。 len can only be applied on lists len只能应用于列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:“int”类型的对象在 python 中没有 len() - TypeError: object of type 'int' has no len() in python TypeError:'function'类型的对象在python中没有len() - TypeError: Object of type 'function' has no len() in python TypeError:类型为&#39;int&#39;的对象没有len()? - TypeError: object of type 'int' has no len()? TypeError:类型为&#39;int&#39;的对象没有len()-Python / Pygame - TypeError: object of type 'int' has no len() - Python/Pygame TypeError:“int”类型的 object 在 python(密码生成器)中没有 len() - TypeError: object of type 'int' has no len() in python (Password Generator) 类型错误:“int”类型的对象没有 len() *减法* - TypeError: object of type 'int' has no len() *subtraction* FLASK PYTHON:TypeError:“int”类型的 object 没有 len() - FLASK PYTHON: TypeError: object of type 'int' has no len() TypeError:“即使我不使用函数“ len()”,“ int类型的对象也没有len()” - TypeError:“object of type 'int' has no len()”, even though I don't use the function “len()” b = [0] * len(st) TypeError: 'function' 类型的 object 没有 len() - b = [0] * len(st) TypeError: object of type 'function' has no len() Def Function 与 for 循环给出错误为“'int'类型的对象没有 len()”,'发生在索引 0' - Def Function with for loop giving error as “object of type 'int' has no len()”, 'occurred at index 0'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM