collections.ChainMap find multiple dictionaries simultaneously

collections.ChainMap that can accommodate several different types of map objects, and then performing key lookup operation, these objects will be treated as a whole is to find one by one, until the key is found.

 

 1 # -*- coding: utf-8 -*-
 2 szServers = {
 3 '192.168.1.1': 'online',
 4 '192.168.1.2': 'online',
 5 '192.168.1.3': 'offline',
 6 '192.168.1.4': 'offline',
 7 }
 8 hzServers = {
 9 '192.168.1.5': ' Online ' ,
 10  ' 192.168.1.6 ' : ' Online ' ,
 . 11  ' 192.168.1.7 ' : ' Offline ' ,
 12 is  ' 192.168.1.8 ' : ' Offline ' ,
 13 is  }
 14  
15  from Collections Import ChainMap
 16 Merged = ChainMap (szServers, hzServers) # plurality of dictionaries combined
 . 17  Print (merged [ ' 192.168.1.1 '])
 18 hz_servers [ ' 192.168.1.9 ' ] = ' Online ' 
19  Print (Merged [ ' 192.168.1.9 ' ]) # interesting is the newly added dictionary will find original elements

Output:

online

online

 

Guess you like

Origin www.cnblogs.com/hell-west-road/p/11332673.html