1, the definition of the Python dictionary
1. Use curly braces {}, separate each key-value pair with a comma, and connect the key to the value with a colon
2, Key: Need immutable data structure, the value can be arbitrary data object
3, the dictionary is unordered, the key in the dictionary must be unique, in the dictionary to value the way is the key to find the corresponding value
A ={}c=dict () a{}c{}b= {'a':'Hello','C':' You','b':' How'}a= [1,2,3]b= ['a','b','C']d= Dict (Zip(b)) d{1:'a',2:'b',3:'C'assign a value to the item value in the dictionary= {1: at}a[1] =3a{1:3} dictionary Access b{'a':'Hello','C':' You','b':' How'}b.get ('a')'Hello'
Add: fruits{'a':'Apple','b':'Banana','g':'Grape','o':'Orange'}fruits.setdefault ('Martin', -) -Fruits.setdefault ('a','appleaaa')'Apple'fruits{'a':'Apple','b':'Banana','g':'Grape','o':'Orange','Martin': -}fruits.update ({3:5}) fruits.update ({'a':'appleupdate'}) fruits{'a':'appleupdate','b':'Banana','g':'Grape','o':'Orange','Martin': -,3:5}2SetDefault: Parameter is key and value, if key does not exist, add key:value, if key exists, do nothing. 3Update: The parameter is key and value, regardless of whether the key exists, it becomes the form of Key:value. Delete (not used in work) fruits{'a':'appleupdate','b':'Banana','g':'Grape','o':'Orange','Martin': -,3:5}fruits.pop (3)5fruits{'a':'appleupdate','b':'Banana','g':'Grape','o':'Orange','Martin': -} change:1, Assignment2, updatea{2:9,3:5,4:9}a[0] =8a{2:9,3:5,4:9,0:8}a[2] =8a{2:8,3:5,4:9,0:8}a.update ({3:Ten}) a{2:8,3:Ten,4:9,0:8} check a{2:8,3:Ten,4:9,0:8}a.get (2)8a[0]8two different values: when the key does not exist, get will not error, the [key] way to fetch the value, will be error
the traversal of a dictionary1、 forIteminch_dict:print Itme2、 forKvincha.items (): Print K,va= {1:3,5:0,'SS':123} forIteminchA:print (item) a= {1:3,5:0,'SS':123} forKvinchA.items (): Print (K,V)
When a dictionary is treated as a sequence, it refers to a sequence of key components .
A dictionary of Python data structures