提问人:Ruchit Vithani 提问时间:4/29/2021 更新时间:4/29/2021 访问量:30
python 中两种不同 2D 数组初始化的区别
Difference between two different ways of 2D array initialisations in python
问:
我正在用 python 解决一个动态编程问题(链接到问题)。我以两种不同的方式初始化 2D DP 数组,并且为这两种方式获得不同的输出。令人惊讶的是,两种初始化都生成相同的数组,但最终输出不同。我确保两种初始化都使用 assert 语句生成相同的数组。
我初始化 2D 数组的两种不同方法如下:
# method 1
dp = [[float('inf') for _ in range(k+1)]for _ in range(n+1)]
# method 2
dp1 = [[float('inf')]*(k+1)]*(n+1)
assert(dp == dp1)
对于以下两个代码片段,我得到两个不同的输出,如下所示。这两个代码片段之间的唯一区别是一行在,而在另一个。dp = [[float('inf') for _ in range(k+1)]for _ in range(n+1)]
dp = [[float('inf')]*(k+1)]*(n+1)
代码 1(链接到此代码片段和输出(单击运行以获取输出)):
# Write your code here
import sys
from heapq import heapify, heappush, heappop ,heappushpop
n,m,k = map(int, input().split())
graph = {u : [] for u in range(n+1)}
for _ in range(m):
u,v,w = map(int, input().split())
graph[u].append((v, w))
graph[v].append((u,w))
dp = [[float('inf') for _ in range(k+1)]for _ in range(n+1)]
# print("DP1 : ", dp)
dp[1] = [0]*(k+1)
# print("DP2 : ", dp)
pq = []
heappush(pq, (0, 1, 0))
while pq:
_, node, par = heappop(pq)
for [next_node, w] in graph[node]:
if next_node != par:
for i in range(k+1):
if (i == 0):
if(dp[next_node][i] > dp[node][i] + w):
dp[next_node][i] = dp[node][i] + w
heappush(pq, (min(dp[next_node]), next_node, node))
else:
if dp[next_node][i] > dp[node][i-1] or dp[next_node][i] > dp[node][i] + w:
dp[next_node][i] = min(dp[node][i-1], dp[node][i] + w)
heappush(pq, (min(dp[next_node]), next_node, node))
# print("DP3 : ", dp)
for i in range(1,n+1):
print(min(dp[i]),end=" ")
print()
输入 1 :
5 6 1
1 2 2
1 3 6
2 4 6
2 5 8
3 5 4
4 5 1
输出 1 :
0 0 0 2 2
代码 2(链接到此代码片段和输出(单击“运行”以获取输出))
# Write your code here
import sys
from heapq import heapify, heappush, heappop ,heappushpop
n,m,k = map(int, input().split())
graph = {u : [] for u in range(n+1)}
for _ in range(m):
u,v,w = map(int, input().split())
graph[u].append((v, w))
graph[v].append((u,w))
dp = [[float('inf')]*(k+1)]*(n+1)
# temp = dp
# dp = dp1
# print("DP1 : ", dp)
dp[1] = [0]*(k+1)
# print("DP2 : ", dp)
pq = []
heappush(pq, (0, 1, 0))
while pq:
_, node, par = heappop(pq)
for [next_node, w] in graph[node]:
if next_node != par:
for i in range(k+1):
if (i == 0):
if(dp[next_node][i] > dp[node][i] + w):
dp[next_node][i] = dp[node][i] + w
heappush(pq, (min(dp[next_node]), next_node, node))
else:
if dp[next_node][i] > dp[node][i-1] or dp[next_node][i] > dp[node][i] + w:
dp[next_node][i] = min(dp[node][i-1], dp[node][i] + w)
heappush(pq, (min(dp[next_node]), next_node, node))
# print("DP3 : ", dp)
for i in range(1,n+1):
print(min(dp[i]),end=" ")
print()
输入 2 :
5 6 1
1 2 2
1 3 6
2 4 6
2 5 8
3 5 4
4 5 1
输出 2 :
0 0 0 0 0
在脚本中,当我使用以下代码初始化两个不同的二维数组,并编写关于它们是否相同的断言时,我惊讶地发现它们都是相同的。在这种情况下,为什么我的程序中两个相同的 2D 数组会得到不同的输出,只是初始化方式不同?
# This part does not produce any assertion error!
dp = [[float('inf') for _ in range(k+1)]for _ in range(n+1)]
dp1 = [[float('inf')]*(k+1)]*(n+1)
assert(dp == dp1)
答: 暂无答案
评论
dp = [[float('inf')]*(k+1)]*(n+1)
对于这种情况,尝试做,结果将是。当您像在第二种方法中一样创建时,似乎第二个数组中的每个元素都具有相同的内存位置dp[0][0] = 5
[[5, inf], [5, inf], [5, inf], [5, inf], [5, inf], [5, inf]]