python update function (20)

 

 

    Previous article   python ChainMap   we introduced the use of built-in functions ChainMap on python, similar to ChainMap functions and update functions are for dictionary operations, but also merge multiple dictionaries, so the question is? ChainMap and update both the difference between what it?

 

About a .update

    python dictionary (Dictionary) update () function of the dictionary dict key / value pairs in the update to another dict.

dict1= {"a":"zhangsan","b":"lisi"}
dict2= {"c":"wangwu"}
 
 
# 合并字典
dict2.update(dict1)
print(dict2)

 

Output:

{ ' C ' : ' wangwu ' , ' A ' : ' zhangsan ' , ' B ' : ' Lisi ' }

 

 

 

Two .update and ChainMap difference

 

    1. Built-in functions ChainMap function merge multiple dictionaries, the merged result memory address has not changed, when we modify the results ChainMap function returns, you will find the original dictionary of data will be the same changes; when I modify the original when the dictionary, the function returns the result ChainMap will follow the changes together, it also means: ChainMap function returns the result of the original dictionary and share a memory address.

# ! Usr / bin / env Python 
# - * - Coding: UTF-8 _ * - 
"" " 
@author: Why grief 
@Blog (personal blog address): shuopython.com 
@WeChat Official the Account (micro-channel public number): Ape said Python 
@Github: www.github.com 
 
@file: python_chainmap.py 
@time: 2019/11/22 21:25 
 
@Motto: short step a thousand miles, no small streams into a mighty torrent, wonderful life program requires constant accumulation! 
"" " 
 
from Collections Import ChainMap 
 
dict1 = { " A " : " zhangsan " , " B " : " Lisi " } 
dict2 = { "c": " Wangwu " } 
 
# combined dictionary 
new_dict = ChainMap (dict1, dict2)
 Print (new_dict) 
 
Print ( " *** " * 20 is )
 # modified data 
new_dict.maps [0] [ " A " ] = " 123 " 
Print ( new_dict)
 Print (dict1)

 

Output:

ChainMap ({ ' A ' : ' zhangsan ' , ' B ' : ' Lisi ' }, { ' C ' : ' wangwu ' })
 ************************************************************ **************************************** 
ChainMap ({ ' A ' : ' 123 ' , ' B ' : ' Lisi ' }, { ' C ' : ' wangwu '})
{' A ' : ' 123 ' , ' b ' : ' lisi ' }

 

2.update primitive function dictionary dict key / value pair to another target dict updated, the dictionary dict after the merger of the original and destination are independent dictionary memory block, both independently of each other!

3.ChainMap function can merge multiple dictionaries at the same time, update function can only merge a dictionary!

 

you may also like:

1.python dictionary dict

2.python ChainMap

3.python dictionary derivations

4.python anonymous function lambda

Abnormal try except 5.python

 

Reproduced please specify: ape say Python  »  Python Update

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/12106716.html