我正在尝试重用字典中的向量,但即使我拉出向量并重命名它,Python 也会修改字典。关于这个问题的任何想法。这是我的代码:
# Set up dictionary
d = {'id 1':[20,15,30]}
d['id 2'] = [5,10,50]
# Pull a vector from the dictionary and decrease the first entry in the vector
vector = d['id 2']
vector[0] = vector[0] - 1
print vector
# Pull the same vector from the dictionary (This is where I want the original vector)
vector2 = d['id 2']
vector2[0] = vector2[0] - 1
print vector2
当我
print vector
# => [4, 10, 50]
当我
print vector2
# => [3, 10, 50]
为什么不重新分配vector2
给原来的[5,10,50]
vector
?我希望这两个都给我[4,10,50]
,但第二个给我[3,10,50]
。