最小和最大类型错误

Min and Max TypeError

由于某些我无法理解的原因,在使用最小和最大函数时发生类型错误。 这是代码: 随机导入 从键入 import List, Any, Union

list1 = []

for i in range(0,200):
    x = random.randint(1,50)
    list1.append(x)

list2 = [ j for j in range(0,51)]

list1.append(list2)
print(list1)

del list1[99:130]

print(len(list1))

print(min(list1))

这是 error:TypeError:'list' 和 'int'

实例之间不支持“<”

谢谢:)

这是因为你有一个嵌套列表 (list1) 参见示例

f = [12,3,4,5,[1,2,3]]
In [25]: min(f)                                                                                                                                                                                             
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-d9448f4534d8> in <module>
----> 1 min(f)

TypeError: '<' not supported between instances of 'list' and 'int'

list1.append(list2)list2(这确实是一个列表)作为附加元素添加到 list1 的末尾。您的错误出现是因为 Python 无法找到由多个数字和一个列表组成的列表的最小值。

我认为您想将 list2 中的所有单个数字添加到 list1,以仅包含数字的列表结尾。这可以使用 extend 而不是 append 来完成。