This module implements a number of classes, which are very flexible. Can be used to replace Python's built-in dict, list, tuple, set type. And some features are not available for these built-in types.
On the network to find some information, focusing on the collections module deque, Counter, Defaultdict class
1. Class Deque
Similar to Python's built-in list, but it's a two-way list. Can be manipulated at any end
Help (Collections.deque)
Class Deque (__builtin__.object)
Deque ([iterable[, MaxLen]])--Deque Object
The deque parameter should be a iterable.
Example:
In [1]: import collectionsin [2]: d = collections.deque (' abcdef ') In [3 ]: print ddeque ([' A ', ' B ', ' C ', ' d ', ' e ', ' F ']) in [4]: d.append (' MM ') #追加元素In [5]: print ddeque ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' mm ']) in [6]: d.appendleft (' GG ') #从左追加元素In [7]: print ddeque ([' GG ', ' a ', ' B ', ' C ', ' d ', ' e ', ' f ', ' mm ']) In [8]: d.pop () # pop one element out[8]: ' mm ' in [9]: print ddeque ([' GG ', ' a ', ' B ', ' C ', ' d ', ' e ', ' F ']) in [10]: D.popleft () #从左pop An element out[10]: ' GG ' in [11]: print ddeque ([' A ', ' B ', ' C ', ' d ', ' e ', ' F ']) in [12]: d.extend (' xyz ') #扩展In [13]: Print ddEque ([' A ', ' B ', ' C ', ' d ', ' e ', ' F ', ' x ', ' y ', ' z ']) in [14]: D.extendleft (' 123 ') #左扩展In [15]: print ddeque ([' 3 ', ' 2 ', ' 1 ', ' a ', ' B ', ' C ', ' d ', ' e ', ' F ', ' x ', ' y ', ' z ']) in [16]: d.reverse ( ) #反转In [17]: print ddeque ([' Z ', ' y ', ' x ', ' F ', ' e ', ' d ', ' C ', ' B ', ' A ', ' 1 ', ' 2 ', ' 3 ']) in [22]: d.extend (' 1111 ') in [23]: D.count (' 1 ') #统计一个元素的个数Out [23]: 5in [24]: print ddeque ( [' Z ', ' y ', ' x ', ' F ', ' e ', ' d ', ' C ', ' B ', ' A ', ' 1 ', ' 2 ', ' 3 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ']) in [27]: d.rotate (2) #右旋转In [28]: print ddeque ([' 1 ', ' 1 ', ' z ', ' y ', ' x ', ' F ', ' e ', ' d ', ' C ', ' B ', ' a ',   ' 1 ', ' 2 ', ' 3 ', ' 1 ', ' 1 ']) in [29]: d.rotate ( -2) #左旋转In [30]: print ddeque ([' Z ', ' y ', ' x ', ' F ', ' e ', ' d ', ' C ', ' B ', ' A ', ' 1 ', ' 2 ', ' 3 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ']) in [31]: d.clear () #清空In [32] : print ddeque ([])
In addition, a maxlen parameter is found in the Deque function. The main limitation is the size of this two-way list
In [the]: D = collections.deque (xrange), 5) in [+]: Print Ddeque ([5, 6, 7, 8, 9], maxlen=5) in [36]:
This article is from the "Learning Notes" blog, so be sure to keep this source http://unixman.blog.51cto.com/10163040/1651973
The collections module in Python