gpt4 book ai didi

Python错误类型错误: unorderable types: int() > list()

转载 作者:行者123 更新时间:2023-12-01 05:54:30 26 4
gpt4 key购买 nike

你好,我正在编写和测试一个递归动态编程背包(0-1)(以下代码是主文件和函数)我不断收到错误(在代码之后发布)...我真的对于错误是什么感到困惑,因此我们将不胜感激任何帮助!

主文件

import Knapsack_Test

size = 10
W = 2**size
knapsack = Knapsack_Test.Knapsack_Test()

for i in range(1, 10):
knapsack.recursive_knapsack_test(size, W)
size = size + 100*i

W = 2**size

函数调用(位于 Knapsack_Test.py 类中

def recursive_dynamic_knap(self, v, w, i, W):
n = len(v) - 1
if i == n:
if W < w[n]:
return 0
else:
return v
elif W < w[i]:
return self.recursive_dynamic_knap(v, w, i+1, W)

return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])

错误

Traceback (most recent call last):
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Main.py", line 9, in <module>
knapsack.recursive_knapsack_test(size, W)
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\Knapsack_Test.py", line 19, in recursive_knapsack_test
self.knapsack.recursive_dynamic_knap(v, w, j, W)
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
File "\\minerfiles.mst.edu\dfs\users\asadmb\Desktop\Programming 3\KnapsackClass.py", line 22, in recursive_dynamic_knap
return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v[i])
TypeError: unorderable types: int() > list()

最佳答案

我相信问题在于 return max(self.recursive_dynamic_knap(v, w, i+1,W), self.recursive_dynamic_knap(v, w, i+1, W-w[i]) + v [i])recursive_dynamic_knap 的调用之一返回 v,它是一个列表,作为 max 的参数没有任何意义() 因为它是“不可排序的”(无法明智地判断大于或小于 int)

关于Python错误类型错误: unorderable types: int() > list(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13149989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com