logo

ChainMap in Python

Python contiene un contenitore chiamato ' ChainMap ' che ne incapsula molti dizionari in una unità. ChainMap è membro del modulo ' collezioni '. Esempio: Python3
# Python program to demonstrate  # ChainMap  from collections import ChainMap d1 = {'a': 1 'b': 2} d2 = {'c': 3 'd': 4} d3 = {'e': 5 'f': 6} # Defining the chainmap  c = ChainMap(d1 d2 d3) print(c) 
Produzione:
ChainMap({'a': 1 'b': 2} {'c': 3 'd': 4} {'e': 5 'f': 6}) 
Vediamo le varie Operazioni su ChainMap

Accedi alle operazioni

    chiavi() :-Questa funzione viene utilizzata per visualizzare tutti i chiavi di tutti i dizionari in ChainMap. valori() :-Questa funzione viene utilizzata per visualizzare valori di tutti i dizionari in ChainMap. mappe() :-Questa funzione viene utilizzata per visualizzare chiavi con i valori corrispondenti di tutti i dizionari in ChainMap.
Python3
# Please select Python 3 for running this code in IDE # Python code to demonstrate ChainMap and # keys() values() and maps # importing collections for ChainMap operations import collections # initializing dictionaries dic1 = { 'a' : 1 'b' : 2 } dic2 = { 'b' : 3 'c' : 4 } # initializing ChainMap chain = collections.ChainMap(dic1 dic2) # printing chainMap using maps print ('All the ChainMap contents are : ') print (chain.maps) # printing keys using keys() print ('All keys of ChainMap are : ') print (list(chain.keys())) # printing keys using keys() print ('All values of ChainMap are : ') print (list(chain.values())) 
Produzione :
All the ChainMap contents are : [{'b': 2 'a': 1} {'c': 4 'b': 3}] All keys of ChainMap are : ['a' 'c' 'b'] All values of ChainMap are : [1 4 2] 
  Nota: Si noti che la chiave denominata "b" esiste in entrambi i dizionari, ma solo la prima chiave del dizionario viene considerata come valore chiave di "b". L'ordinamento viene effettuato man mano che i dizionari vengono passati in funzione.

Operazioni di manipolazione

    nuovo_bambino() :-Questa funzione aggiunge un nuovo dizionario all'inizio della ChainMap. invertito() :-Questa funzione inverte l'ordine relativo dei dizionari nella ChainMap.
Python3
# Please select Python 3 for running this code in IDE # Python code to demonstrate ChainMap and # reversed() and new_child() # importing collections for ChainMap operations import collections # initializing dictionaries dic1 = { 'a' : 1 'b' : 2 } dic2 = { 'b' : 3 'c' : 4 } dic3 = { 'f' : 5 } # initializing ChainMap chain = collections.ChainMap(dic1 dic2) # printing chainMap using map print ('All the ChainMap contents are : ') print (chain.maps) # using new_child() to add new dictionary chain1 = chain.new_child(dic3) # printing chainMap using map print ('Displaying new ChainMap : ') print (chain1.maps) # displaying value associated with b before reversing print ('Value associated with b before reversing is : 'end='') print (chain1['b']) # reversing the ChainMap chain1.maps = reversed(chain1.maps) # displaying value associated with b after reversing print ('Value associated with b after reversing is : 'end='') print (chain1['b']) 
Produzione :
All the ChainMap contents are : [{'b': 2 'a': 1} {'b': 3 'c': 4}] Displaying new ChainMap : [{'f': 5} {'b': 2 'a': 1} {'b': 3 'c': 4}] Value associated with b before reversing is : 2 Value associated with b after reversing is : 3