Basic operations of the day5 dictionary

dictionary:

Basic concepts of dictionaries:

  Dictionaries are the only mapping type in python and store data in the form of key-value pairs. Python performs a hash function operation on the key, and determines the storage address of the value according to the result of the calculation, so the dictionary is stored out of order, and the key must be hashable. Hashable means that the key must be an immutable type, such as: number, string, tuple.

  A dictionary is the most flexible built-in data structure type in python besides lists. A list is an ordered association of objects, and a dictionary is an unordered collection of objects. The difference between the two is that the elements in the dictionary are accessed by key, not by index

1 increase:

 

dict = { ' name ' : ' wangjifei ' , ' age ' :27, ' hometown ' : ' xingtai ' }
dict[ ' job ' ] = ' IT ' ​​#Overwrite if there is, add if not 
print (dict) # {'name': 'wangjifei', 'age': 27, 'hometown': 'xingtai', 'job': 'IT'} 
# setdefault adds a key-value pair to the dictionary. If there is only a key, the corresponding value is none, but if there is a set key-value pair in the original dictionary, it will not be changed or overwritten. 
dict.setdefault( ' hometown ' , ' xingtai ' )
 print (dict) # {'name': 'wangjifei', 'age': 27, 'hometown': 'xingtai', 'job': 'IT'} 
dict. setdefault( ' name '(dict)#{'name': 'wangjifei', 'age': 27, 'hometown': 'xingtai', 'job': 'IT'}

 2. Delete

dict = { ' name ' : ' wangjifei ' , ' age ' :27, ' hometown ' : ' xingtai ' }
s = dict.pop( ' age ' ) #return the deleted value 
s2 = dict.pop( ' hobby ' , ' no return no ' ) #If the deleted key does not exist, you can return any information added, if there is no information, an error will be reported 
print (dict)
 print (s)
 print (s2)
dict = { ' name ' : ' wangjifei ' , ' age ' :27, ' hometown ' : ' xingtai ' }
s1 = dict.popitem() #Randomly delete a key-value pair in the dictionary, and return the deleted key-value pair as a tuple 
print (s1) # ('hometown', 'xingtai') 
print (dict) # { 'name': 'wangjifei', 'age': 27} 
del dict[ ' name ' ] #No return value 
del dict #Delete the entire dictionary

3. Change:

dict = { ' name ' : ' wangjifei ' , ' age ' :27, ' hometown ' : ' xingtai ' }
dic = {'name':'gaoliang','hobby':'girl'}

dict[ ' name ' ] = ' gaoliang ' # if it is overwritten, add 
print (dict) # {'name': 'gaoliang', 'age': 27, 'hometown': 'xingtai'} 
dic = { ' name ' : ' gaoliang ' , ' hobby ' : ' girl ' }
dict.update(dic) #Update of two dictionaries, overwrite all key-value pairs in dic and add them to dict, dic remains unchanged 
print (dict) # {'name': 'gaoliang', 'age': 27 , 'hometown': 'xingtai', 'hobby': 'girl'} 
print (dic) # {'name': 'gaoliang', 'hobby': 'girl'}

4. Check:

dict = { ' name ' : ' wangjifei ' , ' age ' :27, ' hometown ' : ' xingtai ' }
 print (dict[ ' name ' ]) # wangjifei#key value search 
print (dict.get( ' age ' ) ) # 27 # Built-in function search, find the return value. Return None if there is no key 
print (dict.get( ' hobby ' , ' no such key ' )) #Without this key #key returns the corresponding value in the dictionary, returns the added content if not, returns None if there is no added content
dict = { ' name ' : ' wangjifei ' , ' age ' :27, ' hometown ' : ' xingtai ' }

print(dict.keys(),type(dict.keys()))
print(dict.values(),type(dict.values()))
print(dict.items(),type(dict.items()))

》》》

dict_keys(['name', 'age', 'hometown']) <class 'dict_keys'>
dict_values(['wangjifei', 27, 'xingtai']) <class 'dict_values'>
dict_items([('name', 'wangjifei'), ('age', 27), ('hometown', 'xingtai')]) <class 'dict_items'>

The for loop iterates over the lookup dictionary:

The first:

dict = {'name': 'wangjifei','age':27,'hometown':'xingtai'}
for i,v in dict.items():
    print(i,v)

》》》

name wangjifei
age 27
hometown xingtai

The second:

dict = {'name': 'wangjifei','age':27,'hometown':'xingtai'}
for i in dict:
    print(i,dict[i])

》》》

name wangjifei
age 27
hometown xingtai

Separate assignment method:

a,b = 1,3
print('a',a)
print('b',b)
a,b
= b, a print('a',a) print('b',b)

》》》

a 1
b 3
a 3
b 1

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325241092&siteId=291194637