0x01 Clear
The clear method clears all items in the dictionary. This is an in-place operation with no return value
1>>> d = {}2>>> d['name'] ='Gumby'3>>> d[' Age'] = 134>>>D5{' Age': 13,'name':'Gumby'}6>>> value =D.clear7>>>Printvalue8<built-inchMethod clear of Dict object at 0x7f8f905c37f8>
0x02 get
Get (key[, default])
The Get method is a more permissive way to access dictionary entries. In general, if you attempt to access an item that does not exist in the dictionary, an error occurs, and a get does not, and you can customize the default value when the access item does not exist.
1>>> d = {}2>>> d['name']3 Traceback (most recent):4File"<stdin>", Line 1,inch<module>5Keyerror:'name'6>>>PrintD.get ('name')7 None8>>>PrintD.get ('name','Default_value')9Default_value
The Get method can also be used to do some statistics, such as counting the number of each character
1 #!/usr/bin/env python2 #Coding=utf-83 4str =" "5 aaaaaaaaaaaaaa6 bbbbbbbbbbbbb7 CCCCCCCCCCCCCCCC8 dddddddddddddd9 33333333333Ten 44444444444 One `````````````` A " " - -Counts = {} the - forCinchStr: -COUNTS[C] = Counts.get (c, 0) + 1 - + Printcounts - + """ A {' A ': +, ' ': +, ' C ': +, ' B ': +, ' d ': +, ' \ n ': 8, ' 3 ': One, ' 4 ': one} at """
Python Dictionary method