Python basics 2-dictionary, python2-dictionary
Dictionary
Dictionary is another variable container model that can store any type of objects.
Each key-value (key => value) pair of the dictionary uses the colon (:), And each pair is separated by a comma (,The entire dictionary is included in curly brackets ({})Medium
Syntax:
1 goods = {2 'apple':4.5,3 'orange':2.3,4 'banana':3.55 }
Dictionary features:
- Dict is unordered
- The key must be unique.
1. Add Elements
1 >>> goods2 {'apple': 4.5, 'banana': 3.5, 'orange': 2.3}3 >>> goods['peach']=5.04 >>> goods5 {'apple': 4.5, 'banana': 3.5, 'orange': 2.3, 'peach': 5.0}
2. Modify
1 >>> goods ['apple'] = 5.5 # change the price of apple to 5.52 >>>> goods3 {'apple': 5.5, 'Banana ': 3.5, 'Orange ': 2.3, 'peach ': 5.0}
3. delete an element
1 >>> goods 2 {'apple': 5.5, 'Banana ': 3.5, 'Orange': 2.3, 'peach ': 5.0} 3 >>> goods. pop ('peach ') # Delete the last one and return the value 4 5.0 5 >>> goods 6 {'apple': 5.5, 'bana': 3.5, 'Orange ': 2.3} 7 8 >>> del goods ['banana'] # Delete the specified key 9 >>>> goods10 {'apple': 5.5, 'Orange ': 2.3} 11 12 >>> goods13 {'apple': 5.5, 'grape': 6.0, 'Orange ': 2.3, 'watermelon': 12.0} 14 >>> goods. popitem () # Delete 15 randomly ('apple', 5.5) 16 >>> goods17 {'grape': 6.0, 'Orange ': 2.3, 'watermelon': 12.0}
4. Search
1 >>> goods 2 {'grape': 6.0, 'Orange ': 2.3, 'watermelon ': 12.0} 3 >>> 'grape' in goods 4 True 5 >>> goods. get ('Orange ') # know the key, obtain value 6 2.3 7 >>> goods ['watermelon'] 8 12.0 9 >>> goods ['apple'] # If a key does not exist, an error is returned, but get does not, if this parameter does not exist, only None10 Traceback (most recent call last): 11 File "<stdin>", line 1, in <module> 12 KeyError: 'apple' is returned'
5. Multilevel dictionaries and operations
1 area = {'zhejiang ': 2 {3 'hangzhou': ['xihu district', 'xiacheng district ', 'xiaoshan district'], 4 'jiaxing ': ['nanhu district ', 'xiuzhou district ', 'pinghu City'], 5 'wenzhou ': ['jiuwan district', 'lucheng district ', 'dongtou district'] 6}, 7 'jiangsu ': 8 {9 'nanjing ': ['gulou district', 'region ', 'qinhuai district'], 10' Suzhou ': ['gusu district', 'wuzhong district ', 'huqiu district '], 11 'changzhou': ['wujin district ', 'jintan City ', 'xiangyang City '] 12} 13 14 15} 16 17> area ['zhejiang'] ['hangzhou'] 18 ['xihu district ', 'xiacheng district ', 'xiaoshan district '] 19 20 21 >>> area ['zhejiang'] ['hangzhou'] [1] + = ', very large '22> area ['zhejiang '] ['hangzhou'] 23 ['xihu district', 'xiacheng district, dadao district ', 'xiaoshan district']
6. other usage
1 >>> area. keys () 2 dict_keys (['zhejiang ', 'jiangsu']) 3 >>> goods. values () 4 dict_values ([6.0, 2.3, 12.0]) 5 >>> goods. keys () 6 dict_keys (['grape', 'Orange ', 'watermelon']) 7 8 {'grape': 6.0, 'Orange': 2.3, 'watermelon ': 12.0} 9 >>> B = {, 'grape': 4.5} 10 >>> goods. update (B) 11 >>> goods12 {1: 2, 3: 4, 'grape': 4.5, 'Orange ': 2.3, 'watermelon ': 12.0} 13 14 >>> goods. items () 15 dict_items ([(1, 2), (3, 4), ('grape', 4.5), ('Orange ', 2.3), ('watermelon ', (12.0)])
7. traverse the dictionary
1 for key in goods: 2 print (key, goods [key]) 3 # Or 4 for k, v in goods. items (): 5 print (k, v)