Five, python development dictionary

1. Set up a dictionary:
info = {
        'stu1101': 'pang',
          'stu1102' : 'jia',
          'stu1103'   :  'li'
}
 
 
2. Add value to the dictionary:
info.update('stu1104'='cai','stu1105'='wang')
 
3. Delete the value in the dictionary:
 
    1. Use del to delete:
    del info ['stu1101']
    
    2. Use pop to delete:
    info.pop('stu1101')
    
    3. Use ppitem to delete:
    info.popitem() #This is to delete a value randomly
 
Fourth, modify the value in the dictionary:
info['stu1101'] = 'liu'
 
5. Query the value in the dictionary
print(info['stu1101'])
 
Six, the cycle of the dictionary:
1. Find value by key (efficient)
for    i    in    info:
    print(i,info[i]) #Efficient, look up value by key
 
2. Save the key and value in the memory and print it out (inefficient, when the data is too large, the memory will burst)
for    k,v    in    info:
    print (k, v)
 

Guess you like

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