Python의 체인맵

Python에는 ''라는 컨테이너가 포함되어 있습니다. 체인맵 '는 많은 것을 담고 있다. 사전 하나의 단위로. ChainMap은 모듈 '의 멤버입니다. 컬렉션 '. 예: 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  )   
산출:
ChainMap({'a': 1 'b': 2} {'c': 3 'd': 4} {'e': 5 'f': 6})  
ChainMap의 다양한 Operation을 살펴보겠습니다.

액세스 작업

    키() :- 이 기능은 모든 항목을 표시하는 데 사용됩니다. 열쇠 ChainMap의 모든 사전 중 값() :- 이 기능은 표시하는 데 사용됩니다. 가치 ChainMap의 모든 사전 중 지도() :- 이 기능은 표시하는 데 사용됩니다. 해당 값이 있는 키 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  ()))   
출력 :
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]  
  메모 : 'b'라는 키가 두 사전 모두에 존재하지만 첫 번째 사전 키만 'b'의 키 값으로 사용됩니다. 사전이 함수에 전달되면 순서가 지정됩니다.

조작 조작

    new_child() :- 이 함수는 ChainMap의 시작 부분에 새 사전을 추가합니다. 역방향() :- 이 함수는 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'  ])   
출력 :
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