Three kinds of python dictionary creation method

# Create an empty dictionary 
empty_dict = dict () 
Print (empty_dict) 

# with ** variable kwargs keyword argument to create a dictionary 
A = dict (One = 1 , TWO = 2 , Three = 3 ) 
Print (A) 

# pass the iterables 
B = dict (ZIP ([ ' One ' , ' TWO ' , ' Three ' ], [ . 1 , 2 , . 3 ])) 
Print (List (ZIP ([ ' One ' , ' TWO ' , ' Three '], [ . 1 , 2 , . 3 ]))) 
Print (B) 

# incoming iterables 
C = dict ([( ' One ' , . 1 ), ( ' TWO ' , 2 ), ( ' Three ' , . 3 ) ]) 
Print (C) 

C1 = dict ([( ' One ' , . 1 ), ( ' TWO ' , 2 ), ( ' Three ' , . 3 ), ( ' Three ' ,. 4 ), ( ' Three ' , . 5 )]) 
Print (C1) # If there are duplicate key, a value of the final value of duplicates. 


# Map the incoming object dictionary creation dictionary   
D = dict ({ ' One ' : . 1 , ' TWO ' : 2 , ' Three ' : . 3 }) 
Print (D) 

Print (A == B == C == D) 
copy the code 
output: 

{} 
{ ' One ' : . 1 , ' TWO ' : 2 , ' Three': 3}
[('one', 1), ('two', 2), ('three', 3)]
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': . 3 } 
{ ' One ' : . 1 , ' TWO ' : 2 , ' Three ' : . 5 } 
{ ' One ' : . 1 , ' TWO ' : 2 , ' Three ' : . 3 } 
True 
Copy Code

 

Guess you like

Origin www.cnblogs.com/wutanghua/p/11311330.html