Article From:https://www.cnblogs.com/songzijian/p/8985754.html

1. Classification of data types:

   Variable (hash): list, dict, set, list = [11,22,33]

   Immutable (hash): STR, tuple, int, bool, name =’alex’

  Container class:

    Container class data type: list, tuple (tuple), dict (Dictionary)

    Non container data type: STR int bool

Two, dictionary {key (key):value (value)}

  The key (key) of the dictionary must be an immutable data type and is unique.

     hash(Hashi) algorithm:

      The keys of the dictionary are converted to numbers through the hash table, using a two point lookup to find the number (two points search: Set 100 range check 75, it will cut half 50 control 75, smaller than it is 50-100 and half to 75).

  The value (value) of a dictionary can be any data type.

  Dictionary query speed is very fast (hash algorithm), storing a large number of relational data.

Three. There are two kinds of increment

  1、Direct addition (equivalent to index).

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
dic['sex'] = 'male'
print(dic)

{'name': 'Old boy', 'age': 56, 'hobby': 'women', 'sex': 'male'}

 

  2、setdefault  【Format: dictionary.Setdefault (key, value)

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
dic.setdefault('name','alex')
print(dic)

{'name': 'Old boy', 'age': 56, 'hobby': 'women'}   #No cover

 

Four.

  1、pop Directly delete the “key” has a return value, the return value is the value of the corresponding value of the key [format: dictionary.Pop (key, custom “if there is no key to delete, return, if you do not write the wrong, the default is generally None)]

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic.pop('name'))
print(dic)

Old boy#Return value
{'age': 56, 'hobby': 'women'}               #nameDelete success

dic = {'name':'Old boy','age':56,'hobby':'women'}
dic.pop('xxx')                              #There is no "XXX" in the dictionary.

KeyError: 'xxx'                             #Report errors

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic.pop('xxx','No this key'))              #There is no "XXX" in the dictionary.
print(dic)

No this key#Return value
{'name': 'Old boy', 'age': 56, 'hobby': 'women'}   #No mistake

 

 

 

  2、popitem  Random delete, return value, return value is tuple, which is deleted key value pair (generally defaults to the last one).

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic.popitem())
print(dic)

('hobby', 'women')                #Return value: form of tuple{'name': 'Old boy', 'age': 56}

 

  3、clear Clearing DICTIONARY [format: dictionary.Clear ()]

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
dic.clear()
print(dic)

{}

 

  4、del

    1、Delete the entire dictionary from the memory level [format: del dictionary]

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
del dic
print(dic)

NameError: name 'dic' is not defined  #Dictionaries do not exist.

 

    2、Delete key value according to key [del DICTIONARY [delete key]]

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
del dic['age']                        #Delete "age"
print(dic)

{'name': 'Old boy', 'hobby': 'women'}   #Delete success

 

Five, change [accurate, only one, because the second is dictionary to dictionary changes].

  1、Direct plus [have a cover, no add]

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
dic['age'] = 33                                 #Change the 56 of age to 33
print(dic)

{'name': 'Old boy', 'age': 33, 'hobby': 'women'}  #Amend the success

 

  2、update  The update of two dictionariesAdd all key value pairs in the “dictionary” to the “dictionary” outside, and the “dictionary” remains unchanged.

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
zidian = {'name':'alex','sex':'male'}
zidian.update(dic)           #Add all key value pairs in the DIC to the dictionary, and DIC remains unchanged.
print(dic)                   #{'name': 'Old boy ','age': 56,'hobby':'women'}
print(zidian)                 #{'name': 'Old boy,'sex':'male','age': 56,'hobby':'women'}

{'name': 'Old boy', 'age': 56, 'hobby': 'women'}
{'name': 'Old boy', 'sex': 'male', 'age': 56, 'hobby': 'women'}

 

 

 

Six.

  1、Direct view, [format: Dictionary [key you want to see]]If you don’t have the keys you want to check, you will be wrong

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic['name'])

Old boy
dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic['name1'])                 #There is no "name1" in the dictionary.

KeyError: 'name1'                   #Report errors

  2、get  【Format: dictionary.Get (the key you want to see, the default value is None)] if you don’t find it, you won’t report it wrong.

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic.get('name'))

Old boyDIC= {'name':'Old boy','age':56,'hobby':'women'}
print(dic.get('name2'))             #There is no "Name2" in the dictionary.

None                                #No mistake

 

 

 

  3、for Cyclic query

    Dictionary.Keys (),Dictionary.Values (),Dictionary.Items ()  They form a type of list, but not a list. 

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic.keys() , type(dic.keys()))

dict_keys(['name', 'age', 'hobby']) <class 'dict_keys'>

     The dictionary.Items () is placed in a tuple

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
print(dic.items() , type(dic.items()))

dict_items([('name', 'Old boy'), ('age', 56), ('hobby', 'women')]) <class 'dict_items'>

 

    【Format: for I in dictionary.Values (or others).

 

dic = {'name':'Old boy','age':56,'hobby':'women'}
for i in dic.keys():
       print(i)

name
age
hobby

 

     【Transformation: List = list (Dictionary.Keys) * * * *

dic = {'name':'Old boy','age':56,'hobby':'women'}
li = list(dic.keys())
print(li,type(li))

['name', 'age', 'hobby'] <class 'list'>

 

 

Seven, separately assigning

  a = 1,b = 2   print(a,b)==?

 

a , b = 1 , 2                    #It can be understood as a = 1; b = 2
print(a,b)
a , b = ('alex' , '34')          #Empathy
print(a,b)
a , b = ['alex' , '34']          #Empathy
print(a , b)

1 2
alex 34
alex 34

#There can be no following.

a , b = 1 , 2 ,4
print(a,b)                        #Make a mistake

#or
a , b = 1 , 2
print(a,b,c)                      #Make a mistake

 

Eight. The nesting of the dictionary

 dic = {
    ‘name_list’: [‘The bowl of Rong ‘Junjun’.
    ‘status’: None,
    ‘personal_msg’: {
        ‘name’: ‘The reverse teaching material ‘,
        ‘age’: 25,
        ‘hobby_list’:[‘Smoking ‘,’ drinking ‘,’ hot head ‘,
    }
}
# 1,Add a key value pair to DIC, School: old boy
# 2,Change the value of the corresponding status to True
# 3,Append an element to the corresponding list of name_list: Ping Ping
# 4,Add a key pair to the dictionary corresponding to personal_msg: sex: male.
# 5,Turn the hobby_list’s corresponding list to a song. 

 

dic['School'] = 'Old boy'
print(dic)
dic['status'] = True
print(dic)
dic['name_list'].append('Average')
print(dic)
dic1 = dic['personal_msg']
dic1['sex']='male'
print(dic1)
dic['personal_msg']['hobby_list'][2]='Sing'
print(dic)

 

Leave a Reply

Your email address will not be published. Required fields are marked *