我想重新使用字典中的矢量,但是Python修改字典,即使我拉動矢量並重新命名它。圍繞這個問題的任何想法。這裏是我的代碼:如何在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]
。
這樣一個簡單的解決方案。非常感謝。 – myname
不客氣。隨時接受答案。 :) –