Python- dictionary Dict

Dictionary (Dictionary)

  Key must be immutable, it may be numbers, strings or tuples to act as, a list of not

Dictionary traversal

  1, traverse Key

. 1 dict1 = { " red ball " : 5, " basketball " : 3, " yellow ball " :. 4 }
 2  for I in dict1:
 3      Print (I)  
  # output as
red ball
basketball
yellow ball

   

     ( - if the same number of bits per Key, Key can each character separately.)

. 1 dict1 = { " red ball " : 5, " basketball " : 3, " yellow ball " :. 4 }
 2  for I, B in dict1:
 3      Print (I ":", B) 
  # output is as follows:
red: Ball
Basket: ball
yellow: ball

  

  2, traversing values

. 1 dict1 = { " red ball " : 5, " basketball " : 3, " yellow ball " :. 4 }
 2  for I in dict1.values ():
 3      Print (I) 
  # output is as follows:
5
3
. 4

  

  3, traverse the dictionary entry. [Get] tuple type

 

. 1 dict1 = { " red ball " : 5, " basketball " : 3, " yellow ball " :. 4 }
 2  for I in dict1.items ():
 3      Print (I)
 . 4  Print (type (I)) 
  output is as follows:
( 'red ball', 5)
( 'basketball', 3)
( 'yellow ball',. 4)
<class 'tuple'>

 

   

     ( - Traverse key dictionary entry.)

= {Dict1. 1 " red ball " : 5, " basketball " : 3, " yellow ball " :. 4 }
 2 for I, B in dict1.items ():
 3      Print (I, B)
 . 4 Print (type (I) ) 
  # output is as follows:
red ball 5
basketball 3
yellow ball. 4
<class 'STR'>

 

 

 

 

Guess you like

Origin www.cnblogs.com/simplecat/p/11273172.html