Python dictionary Precautions

# 代码一
list1 = []
dict1 = {}
for i in range(5):
    dict1 = {"one": i}
    list1.append(dict1)
print(list1) # [{'one': 0}, {'one': 1}, {'one': 2}, {'one': 3}, {'one': 4}]

# 看一下列表的id值
for id_value in map(lambda x: id(x), list1):
    print(id_value)
I understand
  • Outside the loop creates an empty dictionary and a variable and assignment. We know that in Python, an object is actually a reference to the assignment of an object that is, when one variable to another variable assignment, transfer of address, so will dict1this variable and {}the empty dictionary bundled together, or dict1pointing{}

  • Every time the dictionary is created in the for loop {"one": i}will open up a new space in memory used to store the dictionary, and each of the assignment and will dict1this variable to point to the new dictionary as{"one":0}

  • Then list1carry out appendthe operation, I guess (already confirmed) append operation is carried out, is actually added 该对象的引用, so every time will append each dictionary (references) memory address of the incominglist1

  • Look at list1the id (it can return to uniquely identify an object) ['1593305955784','1593305955496','1593306802432'...], pay attention to the inside of the id are not the same

  • The list contains the memory address of the various dictionaries, thus generating a final list is a list of the respective values ​​are not the same

 
 
 

# 代码二
list2 = []
dict2 = {}
for i in range(5):
    dict2["one"] = i
    print(dict2) # 依次输出 {'one': 0},{'one': 1},{'one': 2},{'one': 3},{'one': 4}
    list2.append(dict2)
print(list2) # [{'one': 4}, {'one': 4}, {'one': 4}, {'one': 4}, {'one': 4}]

# 看一下列表的id值
for id_value in map(lambda x: id(x), list1):
    print(id_value)

I understand

  • The output can be seen, dict2["one"] = ieach time the dictionary just dict2modify the final result of the last modification retained, and not as a new dictionary as a Code
  • Try to analyze the process
    • First open a dictionary and assigned to the venting area in memory dict2(reference)
       
    • Modifying each cycle dict2piece pointed region
       
    • After the dict2memory pointed to by the address append to the dictionary memory list2in
       
    • But every time dict2["one"] = iand no new dictionary is always in the dictionary valuerewriting, and no new dictionary, a dictionary is only beginning to end, so every time append is the same memory address
       
    • We can use to look at this list of id , ['2966419766368','2966419766368','2966419766368'...], found that id is the same
       
    • Because since a value corresponds to only one key, a key multiple times into the value, the value of the latter will overwrite the front, so the final list2value of the value of the last modification to the dictionary to prevail. The list contains the same memory address, thus generating a final list is a list of respective values are the same

 
Shallow copy copy only the object reference, encounter this situation can be resolved deep copy
 
 
above content represents only personal views, not authoritative, look like ...

Guess you like

Origin www.cnblogs.com/NoTrace/p/12580652.html