程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Encyclopedia of Python dictionary merging methods

編輯:Python
old = {
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump '}
new = {
' Fujian warship ': ' Ejection '}
today = {
}
today.update(old)
today.update(new)
print(f'update Method :{
today}')
today = {
**old, **new}
print(f' Dictionary splitting method :{
today}')
today = dict(**old, **new)
print(f' Split the two dictionaries into dict Method :{
today}')
# notes : All keys in both dictionaries must be strings , Because I want to be dict The key value of the function is passed to the parameter 
today = dict(old, **new)
print(f' Split a dictionary into dict Method :{
today}')
# notes : At this point, it is only necessary that all keys of the second dictionary are strings 
today = dict(list(old.items()) + list(new.items()))
print(f' The method of converting a list to a binary and then to a dictionary :{
today}')
today = dict(old.items() | new.items())
print(f'dict+items Of | Operation method :{
today}')
today = dict(old | new)
print(f'dict+ Dictionary | Operation method (Python3.9 And above ):{
today}')
today = old | new # It doesn't need to be dict
print(f' Dictionary | Operation method (Python3.9 And above ):{
today}')
today = {
k: v for dic in [old, new] for k, v in dic.items()}
print(f' Derivation method :{
today}')
from itertools import chain
today = dict(chain(old.items(), new.items()))
print(f'chain Method :{
today}')
from collections import ChainMap
today = dict(ChainMap(old, new))
print(f'ChainMap Method :{
today}')
# notes :ChainMap A duplicate key appears in the dictionary of , The value corresponding to the previous key shall prevail , Let's look at the following example 
new={
' Shandong ship ': ' Ejection ',' Fujian warship ': ' Ejection '}
today = dict(ChainMap(old, new))
print(f'ChainMap Method ( The type of Shandong warship has not been modified successfully ):{
today}')

Output :

update Method :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
Dictionary splitting method :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
Split the two dictionaries into dict Method :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
Split a dictionary into dict Method :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
The method of converting a list to a binary and then to a dictionary :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
dict+items Of | Operation method :{
' Fujian warship ': ' Ejection ', ' Shandong ship ': ' Slip jump ', ' Liaoning ship ': ' Slip jump '}
dict+ Dictionary | Operation method (Python3.9 And above ):{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
Dictionary | Operation method (Python3.9 And above ):{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
Derivation method :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
chain Method :{
' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection '}
ChainMap Method :{
' Fujian warship ': ' Ejection ', ' Liaoning ship ': ' Slip jump ', ' Shandong ship ': ' Slip jump '}
ChainMap Method ( The type of Shandong warship has not been modified successfully ):{
' Shandong ship ': ' Slip jump ', ' Fujian warship ': ' Ejection ', ' Liaoning ship ': ' Slip jump '}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved