Hello everyone, the editor is here to answer the following questions for you, how to use dictionaries in python, and common methods of python dictionaries. Let us take a look today!
1. Create a dictionary
Description: Generate dictionary
grammar:
dic={ 'k1':'v1'}
Example:
dic=dict(k1='v1',k2='v2') dic={ 'k1':'v1','k2':'v2'}
2. Get the key value get
Description: Get the key value. If there is no key in the dictionary, output the custom field. The default is none.
grammar:
def get(self, k, d=None): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass
Example:
dic={ ' k1 ' : ' v1 ' , ' k2 ' : ' v2 ' } a =dic.get( ' k1 ' ) v1 c =dic.get( ' k3 ' ) none #There is no such key in the dictionary, and none is displayed c=dic.get( ' k3 ' , ' no ' ) no #There is no such key in the dictionary, and no is displayed
3. Get the value of the key
Description: Get the key in the dictionary
grammar:
def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> a set-like object providing a view on D's keys """
pass
Example:
dic=dict(k1= ' v1 ' ,k2= ' v2 ' ) dic.keys() for k in dic.keys(): print (k) result: k1, k2 #Display the result
4. Get key value
Description: Get the key value in the dictionary
grammar:
def values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
pass
Example:
dic=dict(k1='v1',k2='v2') for v in dic.values(): print(v) v=v1,v2 #结果
5. Get the value of the key-value pair
Description: Get the key-value pairs in the dictionary
grammar:
def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items """
pass
Example:
dic={ 'k1':'v1','k2':'v2'} for k,v in dic.items(): print(k,v) #结果 k1 v1 k2 v2
6. Delete key-value pairs
Description: Delete a key in a dictionary
grammar:
def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
pass
Example:
dic=dict(k1='v1',k2='v2',k3='v3') dic.pop('k2') print(dic) dic={ 'k3': 'v3', 'k1': 'v1'} #结果
7. Update key-value pairs
Description: Update keys in the dictionary
grammar:
def update(self, E=None, **F): # known special case of dict.update
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
pass
Example:
dic=dict(k1='v1',k2='v2',k3='v3') dic.update({ 'k4':'v4'}) print(dic) dic={ 'k4': 'v4', 'k3': 'v3', 'k1': 'v1', 'k2':v2' ' } #result _
8. Update key value
Description: Update keys in the dictionary
grammar:
dict[key]=value
Example:
dic=dict(k1='v1',k2='v2',k3='v3') dic['k1']=1 print(dic) dic={ 'k3': 'v3', 'k1': 1, 'k2': 'v2'} #结果
Reprinted at: https://www.cnblogs.com/xujianghua/p/6399312.html