将新的随机列表添加到嵌套字典中会将最后一次看到的列表分配给所有列表



正如标题中所描述的,我试图将一个带有列表的字典随机分配给父字典。一件意想不到的事情是,当我想把它分配给父字典时,我只分配了最新的字典。请参阅代码示例:

temp = {}
d = {}
for x in range(3):
for i in range(3):
temp[i] = list(np.random.randint(10, size=10))
print(x)
print(temp)
d[x] = temp
print(d[x])
print("--------------------------")
print("The final dictionary: ", d)

0
{0: [1, 4, 1, 7, 3, 9, 3, 8, 1, 5], 1: [6, 5, 2, 0, 5, 0, 5, 9, 7, 1], 2: [7, 9, 3, 3, 5, 8, 6, 0, 6, 0]}
{0: [1, 4, 1, 7, 3, 9, 3, 8, 1, 5], 1: [6, 5, 2, 0, 5, 0, 5, 9, 7, 1], 2: [7, 9, 3, 3, 5, 8, 6, 0, 6, 0]}
1
{0: [0, 0, 7, 0, 0, 1, 9, 9, 2, 0], 1: [1, 5, 8, 7, 2, 9, 0, 8, 7, 9], 2: [8, 8, 4, 8, 1, 1, 1, 2, 4, 4]}
{0: [0, 0, 7, 0, 0, 1, 9, 9, 2, 0], 1: [1, 5, 8, 7, 2, 9, 0, 8, 7, 9], 2: [8, 8, 4, 8, 1, 1, 1, 2, 4, 4]}
2
{0: [5, 4, 7, 1, 1, 3, 0, 3, 6, 9], 1: [5, 9, 7, 8, 0, 7, 1, 7, 6, 9], 2: [4, 2, 6, 1, 3, 7, 2, 6, 4, 8]}
{0: [5, 4, 7, 1, 1, 3, 0, 3, 6, 9], 1: [5, 9, 7, 8, 0, 7, 1, 7, 6, 9], 2: [4, 2, 6, 1, 3, 7, 2, 6, 4, 8]}
--------------------------
The final dictionary:  {0: {0: [5, 4, 7, 1, 1, 3, 0, 3, 6, 9], 1: [5, 9, 7, 8, 0, 7, 1, 7, 6, 9], 2: [4, 2, 6, 1, 3, 7, 2, 6, 4, 8]}, 1: {0: [5, 4, 7, 1, 1, 3, 0, 3, 6, 9], 1: [5, 9, 7, 8, 0, 7, 1, 7, 6, 9], 2: [4, 2, 6, 1, 3, 7, 2, 6, 4, 8]}, 2: {0: [5, 4, 7, 1, 1, 3, 0, 3, 6, 9], 1: [5, 9, 7, 8, 0, 7, 1, 7, 6, 9], 2: [4, 2, 6, 1, 3, 7, 2, 6, 4, 8]}}

您的问题是,在d[x] = temp行中,您只是在引用同一个对象,当您覆盖temp时,您还覆盖到它的所有现有链接。该问题称为shallow vs deep copy。有多种方法可以创建"真实"副本,但copy library提供了两个功能copy, deepcopy。注意,对于嵌套对象,您需要deepcopy函数

from copy import copy

temp = {}
d = {}
for x in range(3):
for i in range(3):
temp[i] = list(np.random.randint(10, size=10))
print(x)
print(temp)
d[x] = copy(temp)
print(d[x])
print("--------------------------")
print("The final dictionary: ", d)
{0: [2, 7, 9, 5, 2, 6, 1, 9, 9, 4], 1: [1, 0, 1, 9, 6, 1, 6, 4, 2, 3], 2: [9, 1, 1, 2, 1, 3, 5, 5, 5, 2]}
{0: [2, 7, 9, 5, 2, 6, 1, 9, 9, 4], 1: [1, 0, 1, 9, 6, 1, 6, 4, 2, 3], 2: [9, 1, 1, 2, 1, 3, 5, 5, 5, 2]}
1
{0: [3, 0, 8, 0, 7, 1, 9, 9, 0, 6], 1: [4, 1, 5, 0, 7, 5, 8, 9, 8, 3], 2: [7, 2, 7, 5, 8, 5, 5, 4, 6, 0]}
{0: [3, 0, 8, 0, 7, 1, 9, 9, 0, 6], 1: [4, 1, 5, 0, 7, 5, 8, 9, 8, 3], 2: [7, 2, 7, 5, 8, 5, 5, 4, 6, 0]}
2
{0: [0, 2, 3, 3, 5, 6, 8, 9, 0, 9], 1: [5, 2, 2, 4, 6, 5, 3, 8, 7, 3], 2: [1, 6, 5, 3, 7, 1, 3, 3, 4, 4]}
{0: [0, 2, 3, 3, 5, 6, 8, 9, 0, 9], 1: [5, 2, 2, 4, 6, 5, 3, 8, 7, 3], 2: [1, 6, 5, 3, 7, 1, 3, 3, 4, 4]}
--------------------------
The final dictionary:  {0: {0: [2, 7, 9, 5, 2, 6, 1, 9, 9, 4], 1: [1, 0, 1, 9, 6, 1, 6, 4, 2, 3], 2: [9, 1, 1, 2, 1, 3, 5, 5, 5, 2]}, 1: {0: [3, 0, 8, 0, 7, 1, 9, 9, 0, 6], 1: [4, 1, 5, 0, 7, 5, 8, 9, 8, 3], 2: [7, 2, 7, 5, 8, 5, 5, 4, 6, 0]}, 2: {0: [0, 2, 3, 3, 5, 6, 8, 9, 0, 9], 1: [5, 2, 2, 4, 6, 5, 3, 8, 7, 3], 2: [1, 6, 5, 3, 7, 1, 3, 3, 4, 4]}}

在我看来,问题是您对所有人都使用相同的temp

所以只需在for x in range(3):之后移动语句temp = {}

import numpy as np
d = {}
for x in range(3):
temp = {}
for i in range(3):
temp[i] = list(np.random.randint(10, size=10))
print(x)
print(temp)
d[x] = temp
print(d[x])
print("--------------------------")
print("The final dictionary: ", d)

最新更新