# ! /Usr/bin/Python # Coding = UTF-8 # Http://docs.python.org/library/collections.html # Count object only 2.7 From Collections Import Counter # Count the number of occurrences of letters Counter ( ' Hello World ' ) Counter ([ ' Red ' , ' Blue ' , ' Red ' , ' Green ' , ' Blue ' ,' Blue ' ]) # A value smaller than or equal to 0 is ignored. C = counter (A = 4, B = 2, c = 0, D =-2 ) List (C. Elements ()) # Take the first three letters Counter ( ' Hello World ' ). Most_common (3 ) # Heap From Collections Import Dequed = Deque ( ' ABC ' ) D. append ( ' D ' ) D. Pop () # Post-in-first-out D. popleft () # First in first out # Returns the last n lines of text. Deque (open (filename), n) # Defaultdict From Collections Import Defaultdict # Use List to initialize a dict D = Defaultdict (list) d [ " Yellow " ]. Append (1 ) D [ " Red " ]. Append (2 ) D [ " Yellow " ]. Append (3 ) Print D. Items () # [('Red', [2]), ('yellow', [1, 3])] # Use int to initiate a dict D = Defaultdict (INT) d [ " Yellow " ] + = 1 D [ " Red " ] + = 2 D [ " Yellow " ] + = 3 Print D. Items () # [('Red', 2), ('yellow', 4)]