我認爲你正在學習蟒蛇,所以你需要不同的方法來複制和字典等字典。
這裏有一些方法本: -
#First step create a dictionary
a = {"first_name":"Jon", "last_name":"Doe"}
#Approach 1: Using List comprehension
b = {key:value for key, value in a.items()}
#Approach 2: Initialise new dictionary and loop first to create new
c = {}
for key, value in a.items():
c[key] = value
#Approach 3:Using Deep copy function
import copy
d = copy.deepcopy(a)
#Approach 4:Using Update function of dictionary
e = {}
e.update(a)
#Finally Print all dicts
print (a, b, c, d, e)
#Below is Output of a,b,c,d,e:-
({'first_name': 'Jon', 'last_name': 'Doe'}, {'first_name': 'Jon', 'last_name': 'Doe'}, {'first_name': 'Jon', 'last_name': 'Doe'}, {'first_name': 'Jon', 'last_name': 'Doe'}, {'first_name': 'Jon', 'last_name': 'Doe'})
你確定你的代碼,甚至跑沒有任何錯誤? –
您嘗試過'new_dictionary [k] = v',並且出現了'new_dictionary。[k] = v'的en錯誤? –
[真的嗎?](http://ideone.com/9PrJSR) –