1.collections in abc
- MutableMapping subclass is Mapping
- Mapping is a subclass of Collection
- Collection is Sized, Iterable, Container subclass
- dict is registered to have the MutableMapping MutableMapping method, dict duck type, equivalent MutableMapping subclass.
. 1 from collections.abc Import MutableMapping 2 . 3 # dict belonging MutableMapping type . 4 . 5 A = {} . 6 Print (the isinstance (A, MutableMapping)) # True
A common use of 2.dict
. 1 A = { " . 1 " : { " A " : " AA " }, 2 " 2 " : { " B " : " BB " }} . 3 . 4 # empty dictionary . 5 a.clear () . 6 . 7 # shallow copy Dictionary Although normally shallow copy can be assigned, but if the value in my_dopy_dict were changed, a value corresponding to the change will be carried out . 8 my_dopy_dict = a.copy () . 9 10 # deep copy of a deep copy is in real the memory of them declare a new variable 11 Import Copy 12 new_dict =copy.deepcopy (A) 13 is 14 # GET function dict.get (to find the key, if the key corresponding to the content not found return data) 15 Print (a.get ( " . 3 " , {. 1: " . 3 " }) ) # {. 1: '. 3'} 16 . 17 # dict.fromkeys () function creates a new dictionary, the sequence elements do seq seq key dictionary may be iterative, value of the dictionary initial value corresponding to all keys . 18 is my_list = [. 1, 2,. 3 ] . 19 my_new_dict = dict.fromkeys (my_list, { " 222 " : " 3434 " }) # {. 1: { '222': '3434'}, 2: { '222': '3434'}, 3: { '222': '3434' Similar setdefault () function and get () method, 22 # If the key does not exist in the dictionary, it will add key and value to the default value. 23 # , if present, will be returned corresponding to the key value 24 a.setdefault ( " . 3 " , " CC " ) # A = { '. 1': { 'A': 'AA'}, '2': { 'B': 'BB'}, '. 3': 'CC'} 25 Print (a.setdefault ( " 2 " , " CC " )) # returns { 'b': 'bb' } 26 is 27 # Update () function is merging two dictionaries: the dictionary dict2 key / value pairs to update in dict. 28 # If the dictionary and b have a same key, the key will be to a value corresponding to the updated 29 #If the dictionary b there are not a key, is a key and the value will No added to it 30 b = { " . 3 " : " CC " , " 2 " : " dd " } 31 is a.update (b) 32 Print (A) # { '. 1': { 'A': 'AA'}, '2': 'dd', '. 3': 'CC'}
3.dict subclass
4.set和frozenset
5.dict and implementation principles set