python_ dictionary dict-related operations

Take the key

dict.setdefault(key, default=None)     
dict.get(key, default=None)

Python dictionary setDefault () method and the like get () method returns the value of the specified key, if the key is not in the dictionary, and the set key will add value to a specified value, the default is None.
get () and setdefault () the difference: setdefault () returns the key if not in the dictionary, it will add key (updated dictionary), and get () does not add keys.

collections.defaultdict([default_factory[, …]])

Python access the dictionary by Key, when Key does not exist, it will lead to 'KeyError' exception. To avoid this, it is possible to use a defaultdict () method in class collections to provide default values ​​for the dictionary. This may be factory_function list, set, str, etc., when the key function is not present, the default value of the return plant function, such as list corresponding to [], corresponding to an empty string STR, SET corresponding to the set (), int corresponds to 0.

Various printing methods

  1. Direct Print
print(dict)
  1. Converted into a sort list Print

Guess you like

Origin www.cnblogs.com/wujingqiao/p/11861075.html