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 '}