One: Dictionary (DIC)
A dictionary is the only type of mapping in Python that stores data in the form of key-value pairs (key-value). Python computes the hash function of key and determines the storage address of value based on the computed result, so the dictionary is stored out of order and the key must be hashed. A hash indicates that a key must be an immutable type, such as a number, a string, a tuple.
The Dictionary (dictionary) is the most flexible built-in data structure type in addition to the list of unexpected python. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
1. Increase
DIC = {"name":"Jin"," Age": 18,"Sex":"male"}#dic[' key ' = ' value ' key exists then change, key does not exist then adddic['name'] ='qwe'Print(DIC) dic['Hobby'] ='Travel'Print(DIC)#output: {' name ': ' Qwe ', ' age ':, ' sex ': ' Male ', ' key ': ' Value '}#output: {' name ': ' Jin ', ' Age ':, ' sex ': ' Male ', ' hobby ': ' Travel '}#SetDefault key exists unchanged, does not exist addDic.setdefault (' Age',' the')Print(DIC) Dic.setdefault ('Hobby','Travel')Print(DIC)#Output Result:#{' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}#{' name ': ' Jin ', ' Age ':, ' sex ': ' Male ', ' hobby ': ' Travel '}
View Code
2. By deleting
DIC = {"name":"Jin"," Age": 18,"Sex":"male"}#Pop by Key Delete, there is a return value if there is no key, you can set the return value (do not set the return value error)Dic.pop ('name')Print(DIC)#Output Result:#{' Age ':, ' sex ': ' Male '}ret= Dic.pop ('Hobby','no This key')Print(ret)Print(DIC)#Output Result:#no This key#{' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}#Clear Empty Dictionarydic.clear ()Print(DIC)#output result: {}delDelete a dictionary, delete a key value pairdeldic Delete Dictionarydeldic['name']Print(DIC)#Output Result:#{' Age ':, ' sex ': ' Male '}popitem random removal of ret=Dic.popitem ()Print(ret)Print(DIC)#Output Result:#(' sex ', ' male ')#{' name ': ' Jin ', ' Age ':
View Code
3. Change
DIC = {"name":"Jin"," Age": 18,"Sex":"male"}dic2= {"name":"Alex","Weight": 75}#dic[' key ' = ' value ' has a key to overwrite, no added#update adds the key-value pair overlay in the Dic2 to DIC, and the DIC does not changedic.update (DIC2)Print(DIC)#Output Result:#{' name ': ' Alex ', ' age ':, ' sex ': ' Male ', ' weight ':
View Code
4. Check
DIC = {"name":"Jin"," Age": 18,"Sex":"male"}Print(dic['name'])#check value directly with key without key errorPrint(dic[' Age'])Print(dic['name'],type (dic['name']))Print(dic[' Age'],type (dic[' Age']))#Output Result:#Jin <class ' str ' >#<class ' int ' >#get the return value when you check the value directly without key by keyPrint(Dic.get ('name'))Print(Dic.get ('name1'))Print(Dic.get ('name1','does not have this key to'))#Output Result:#Jin#None#does not have this key to
View Code
5. Other methods of the dictionary
DIC = {"name":"Jin"," Age": 18,"Sex":"male"}keys=Dic.keys ()Print(Keys,type (keys))#Output Result:#Dict_keys ([' Name ', ' age ', ' sex ']) <class ' Dict_keys ' >Values=dic.values ()Print(values)#Output Result:#dict_values ([' Jin ', ' Male '])Items=Dic.items ()Print(items)#Output Result:#dict_items ([' Name ', ' Jin '), (' Age ', ' + '), (' Sex ', ' male ')]) forKinchDIC:Print(k)" "output Result: Nameagesex" " forVinchDIC:Print(v)" "# Output Result: Nameagesex" " forKvinchDic.items ():Print(k,v)" "# Output: Name jinage 18sex male" " forKinchDIC:Print(K,dic[k])" "# Output: Name jinage 18sex male" "
View Code
6, Dictionary nesting
DIC = { 'name':"Jinxin", 'name_list': [1, 2, 3,'Li Jie'], 1: { 'Python10':['Little Black','Brother Meng'], 'old boy': {'name':'Oldboy',' Age': 56} }}#1,[1,2,3, ' Li Jie '] append ' Wusir 'dic['name_list'].append ('Wusir')Print(DIC)#2, [' Little Black ', ' brother Meng '] small black brother in the middle of inserting a flower brotherdic[1]['Python10'].insert (1,'Flower Brother')Print(DIC)#3, {' name ': ' Oldboy ', ' Age ': 56} Add a key-value pair, hobby: Mandic[1]['old boy']['Hobby'] ='Men'Print(DIC) dic[1]['old boy'].setdefault ('Hobby','Men')Print(DIC)
View Code
DiC of Python data types