First, the definition of the dictionary method:
1. dic = {' name ': ' Karen ', ' age ': $, ' hobby ': ' Girl ', ' Is_handsome ': True}
Print (DIC) #==>{' name ': ' Karen '}
DIC = {' name ': ' Karen ', ' age ': $, ' hobby ': {' name ': ' Xu ', ' Age ': $}, ' Is_handsome ': True}
2, Dic=dict ((' Name ', ' Karen '),) | | Dic=dict ([' Name ', ' Karen '],)) | | Dic=dict ([[' Name ', ' Karen '],]) | | Dic=dict ([' Name ', ' Karen '),])
Print (DIC) #==>{' name ': ' Karen '}
Two main features of the dictionary: disorder, key unique
Dictionary keys can only have immutable elements >>>>> immutable types: Integer, string, tuple mutable type: list, dictionary
Second, increase
1. dic1={' name ': ' Karen '}
dic1[' age ']=18
Print (DIC1) #==>{' name ': ' Karen ', ' Age ': 18}
2, Dic1.sendefault (' Age ', "#如果有这个键值对"), do nothing and return the value, if not, increase and return the value
Print (DIC1)
Third, find
1. dic={' name ': ' Karen ', ' Age ': 18}
Print (dic[' name ']) #==>karen
2, Print (Dic.keys ()) #==> only printing keys
The type is Dict_keys type, not a list
Print (Dic.values ()) #==> prints only values
Print (Dic.items ()) #==> printing key-value pairs
Iv. Change of
1. dic={' name ': ' Karen ', ' Age ': 18}
dic[' age ']=33
Print (DIC) #==>{' name ': ' Karen ', ' Age ': 33}
2. dic={' name ': ' Karen ', ' Age ': 18}
Dic1={' a ': ' AAA ', ' B ': ' BBB '}
Dic.update (DIC1) #若有值则替换
V. Delete
1. dic={' name ': ' Karen ', ' Age ': 18}
del dic (' name ')
Print (DIC) #==>dic={' age ': 18} Delete entire key-value pairs
2, Dic.clear ()
Print (DIC) #==>dic={}
3. Dic.pop (' age ')
Print (DIC) #==>dic={' age ': 18} Deletes the entire key-value pair and returns the value as a return value
4, A=dic.popitem ()
Print (A,dic) #随机删除
Vi. other operations and the methods involved
1, Dic=dict.fromkeys ([' host1 ', ' host2 ', ' host3 '], ' test ')
Print (DIC) #==>{' host1 ', ' host2 ', ' host3 ', ' Test '} Initialize dictionary with keys, no value
2, Dic=[5: ' 555 ', 2: ' 666 ', 4: ' 444 ']
Print (sorted (Dic.items ()))
Print (sorted (Dic.keys ()))
Print (sorted (dic.values ()))
3. dic={' name ': ' Karen ', ' Age ': 18}
For I in DIC:
Print (I,dic[i])
Python 3.0 dictionary Additions and deletions