def func(N, total_weight):
weight = [1, 2, 3, 4]
value = [2, 4, 4, 5]
dp = [[0 for i in range(total_weight+1)] for j in range(N+1)]
print(dp)
# dp = [[0] * (total_weight + 1)] * (N+1)
# print(dp)
for i in range(1, N + 1):
for j in range(1, total_weight + 1):
if j >= weight[i - 1]:
value1 = dp[i - 1][j - weight[i - 1]] + value[i - 1]
dp[i][j] = max(value1, dp[i - 1][j])
else:
dp[i][j] = dp[i - 1][j]
return dp
func(4, 6)
1
pwli 2020-07-13 17:09:43 +08:00
后者是浅拷贝,也就是说列表内的元素(另一个列表)指向同一个地址
|
2
18870715400 OP @pwli 刚尝试了一下, 的确是这样, 谢谢!
|