The basic data types Python3 dict

dict

That dictionary, another model variable containers, may store any type of object.

key: value form, the value of speed

key must be unique and immutable, so do key string, number, tuple

(A Boolean value may be used as key, but may be repeated 1 or 0, the repeat key when leaving only a display, to the front of the overwriting ), may be any type of value may not be unique, not null character

Each dictionary key (key => value) of the colon ( : ) is divided, with between each pair comma ( , ) is divided, in the entire dictionary including curly braces ( {}) in the following format:

1 d1={"name":"garrett","age":123}

Support for loop

Dictionary variable, but the disorder can not be indexing and slicing, but can be a value by key, or use get () method with key values ​​(get () return value can be defined without finding objects)

Delete dictionary support

. 1 D3 = {
 2      11:12 ,
 . 3      " 12 is " : 123 ,
 . 4      True: 124 ,
 . 5      # [1,2]: 12 is, when not List # Key 
. 6      (12,3): 12 is, # tuples can not be modified may be 
7      # {12,3}: # 123 variable dictionary, not when the Key 
. 8      123 : {
 . 9          " A " : " A " ,
 10          " B " : " B " ,
 . 11          " C " : (11,22 )
12 is      }
 13 is  }
 14  # Print (D3 [. 3]) with an index value of not # 
15  Print (D3 [ " 12 is " ]) # use key values 
16  Print (D3 [. 11 ])
 . 17  Print (D3 [123] [ " C " ] [. 1]) # deep value 
18 is  
. 19  del D3 [. 11] # can delete the key according to the key 
20 is  Print (D3)
 21 is  
22 is  del D3 [123] [ " C " ] # deep deleted 
23  Print (D3)

result

1 123
2 12
3 22
4 {'12': 123, True: 124, (12, 3): 12, 123: {'a': 'A', 'b': 'B', 'c': (11, 22)}}
5 {'12': 123, True: 124, (12, 3): 12, 123: {'a': 'A', 'b': 'B'}}

 

 1 d3={
 2     11:12,
 3     "12":123,
 4     True:124,
 5     (12,3):12,
 6     123:{
 7         "a":"A",
 8         "b":"B",
 9         "c":(11,22)
10     }
11 }
12 for item in d3:# The default loop only key output 
13      Print (Item)

result

1 11
2 12
3 True
4 (12, 3)
5 123

Key and value output by built-in method

 1 d3={
 2     11:12,
 3     "12":123,
 4     True:124,
 5     (12,3):12,
 6     123:{
 7         "a":"A",
 8         "b":"B",
 9         "c":(11,22)
10     }
11 }
12 print(d3.keys())
13 print(d3.values())
14 for item1 in d3.values():
15     print(item1)
16 for item2 in d3.keys():
17     print(item2)

result

 1 dict_keys([11, '12', True, (12, 3), 123])
 2 dict_values([12, 123, 124, 12, {'a': 'A', 'b': 'B', 'c': (11, 22)}])
 3 12
 4 123
 5 124
 6 12
 7 {'a': 'A', 'b': 'B', 'c': (11, 22)}
 8 11
 9 12
10 True
11 (12, 3)
12 123

While the output key and value

. 1 D3 = {
 2      11:12 ,
 . 3      " 12 is " : 123 ,
 . 4      True: 124 ,
 . 5      (12,3): 12 is ,
 . 6      123 : {
 . 7          " A " : " A " ,
 . 8          " B " : " B " ,
 . 9          " C " : (11,22 )
 10      }
 . 11  }
 12 is  # a built-in method, and simultaneously outputs key values 
13 is print(d3.items())
14 for k,v in d3.items():#
15     print(k,v)

result

1 dict_items([(11, 12), ('12', 123), (True, 124), ((12, 3), 12), (123, {'a': 'A', 'b': 'B', 'c': (11, 22)})])
2 11 12
3 12 123
4 True 124
5 (12, 3) 12
6 123 {'a': 'A', 'b': 'B', 'c': (11, 22)}

Modifying a dictionary

Way to add new content to the dictionary to add new key / value pairs, modify, or delete existing key / value pairs. Following examples:

. 1 dict1 = { ' the Name ' : ' Runoob ' , ' Age ' :. 7, ' Class ' : ' First ' }
 2 dict1 [ ' Age ' ] =. 8   # Update Age 
. 3 dict1 [ ' School ' ] = " the AWD "   # add information 
. 4  
. 5  Print ( " dict1 [ 'Age']: " , dict1 [ ' Age '])
6 print("dict1['School']: ", dict1['School'])

result

1 dict1['Age']:  8
2 dict1['School']:  AWD

 


 

Dictionary to master the operation:

1. Press the key values ​​access

2. length len

3. The members of the operations in and not in

4. Delete

5.keys () and values ​​() and items () method

6. cycle

 1 d3={
 2     11:12,
 3     "12":123,
 4     True:124,
 5     (12,3):12,
 6     123:{
 7         "a":"A",
 8         "b":"B",
 9         "c":(11,22)
10     }
11 }
12 print(len(d3))
13 print("a" in d3)
14 print("A" in d3)

 

result

1  False
 2 False

Guess you like

Origin www.cnblogs.com/zhangyanlong/p/11326705.html