n = 5
dp = [[0]*5 for i in range(5)]
for i in range(n):
dp[i][i] = 1
for i in dp:
print i
输出:
[1, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]
dp2 = [[0] * 5 ]* 5
for i in range(n):
dp2[i][i] = 1
for i in dp2:
print i
输出:
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
原因:5行数据指向了同一块内存地址,只是显示的时候*5了、、