The dictionary operations python

1. dictionary format

{name = 'k1', 'v1'

'k2':'v2'

}

Dictionary key is disordered, it is not based on index values, based on key values.

2. Add the elements

The first: dic [ 'k3'] = v3 #key present to modify, add does not exist

The second: dic.setdefault ( 'k4', 'v4') #key exist on the same, there is added

3. Modify

dic [ 'k3'] = v3 #key present to modify 

4. Delete

dic.pop(‘k2’)

dock [ 'k2']

dic.popitem () # delete a random key

5. Value

print (dic [ 'k2']) # absent key, given

print (dic.get ( 'k2')) # key, none reported absent

print (dic.get ( 'k2', 'v2')) # absent key, default assignment v2

print (info.keys ()) # Get all the dictionary key value

print (info.values ​​()) # get the value of all dictionary values

6. Other

dic.clear()

dic1.update (dic2) # dictionary to add content to dic1 in dic2

dic.haskey ( 'k1') # python2 determines whether there is key

print ( 'k1' in dic) # python3 whether there is a dictionary key

k + = 1. k = k + 1 represents

 

Guess you like

Origin www.cnblogs.com/Dorami/p/10929808.html