A: Collection library collection
Python has some built-in data types, and the Collections module provides several additional data types:
1,namedtuple generates a tuple subclass that can use a name to access the content of an element
2,deque dual-ended queue, which accelerates the addition and rollout of objects from the other side
3,counter counter, mainly used to count
4,ordereddict ordered Dictionary
5,defaultdict a dictionary with default values
1) namedtuple named Ganso form, generally need to know what the meaning of each field in the meta-ancestor, you can use named meta-ancestor Namedtuple
Inherit the named tuples
2) Deque dual-ended queue
The biggest benefit of deque is the rapid addition and removal of objects from the head of the queue, such as Popleft () and Appendleft ()
Append () appendleft () pop () Popleft () Extend () Extendleft () rotate ()
1, the most used is to limit the queue length, to get the last value of the queue or a few values
2) Other usage of deque, can refer to the following
3) Counter counter
Elements returns an iterator that displays the number of repetitions of the element, if the number of times is less than 1, is ignored
Most_common get the most frequently occurring elements
Subtract the elements of a two element counter
4) Ordereddict ordered dictionary
5) Defaultdict Default dictionary
Two: HEAPD heap queue
-
Heapq.heappush (heap, item)
-
Push the value item onto the heap, maintaining the heap invariant.
-
Heapq.heappop (Heap)
-
Pop and return the smallest item from the heap, maintaining the heapinvariant. If The heap is empty,Indexerror is raised. To access Thesmallest item without popping it, use heap[0].
-
Heapq.heappushpop (heap, item)
-
Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than Heappush () followed by a separate call to Heappop ().
-
Heapq.nlargest (n, iterable, Key=none)
-
Return a list with the n largest elements from the dataset defined byiterable. Key, if provided, specifies a function of one argument that isused to extract a comparison key from each element in the IT Erable:key=str.lower equivalent to:sorted (iterable, key=key,reverse=true) [: n]
-
Heapq.nsmallest (n, iterable, Key=none)
-
Return a list with the n smallest elements from the dataset defined byiterable. Key, if provided, specifies a function of one argument that isused to extract a comparison key from each element in the IT Erable:key=str.lower equivalent to:sorted (iterable, Key=key) [: n]
Python collection and HEAPQ module usage instructions