colletions
是python内置的模块,里面有很多数据类型['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList', 'UserString', 'Counter', 'OrderedDict', 'ChainMap']
,这些数据类型让我们内置的list,dict,tuple,set操作更加方便。# 官方说明
* deque list-like container with fast appends and pops on either end
queue.Queue
先进先出,单向import queue
# 先进先出
q = queue.Queue()
for i in range(10):
q.put(i)
while not q.empty():
print(q.get())
"""
打印结果 0 1 2 3 4 5 6 7 8 9
"""
queue.LifoQueue
后进先出,单向import queue
# 先进后出
q = queue.LifoQueue()
for i in range(10):
q.put(i)
while not q.empty():
print(q.get())
"""
打印结果 9 8 7 6 5 4 3 2 1 0
"""
queue.PriorityQueue
优先级队列,也是先进先出,不过他会帮咱们进行排序
# 优先级队列
import random
import queue
q = queue.PriorityQueue()
for _ in range(10):
num = random.randint(1,10) # 这里是随机的
q.put(num)
while not q.empty():
print((q.get()))
"""
打印结果1 2 2 4 4 7 7 7 8 9
在q.put的时候,它内部会帮咱们对插入队列的值进行排序(升序)
"""
deque
这个双端队列。from colletions import deque
查看源码__init__
方法,第一个参数是可迭代对象,第二个参数是可选的,指定deque的最大长度为None,如果不写,则长度没有限制,maxlen>=0str list set dict tuple等
,deque本身也是一个可迭代对象TypeError
异常append
方法,从右边添加元素 def append(self, *args, **kwargs): # real signature unknown
""" Add an element to the right side of the deque. """
pass
def appendleft(self, *args, **kwargs): # real signature unknown
""" Add an element to the left side of the deque. """
pass
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from the deque. """
pass
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a deque. """
pass
def count(self, value): # real signature unknown; restored from __doc__
""" D.count(value) -> integer -- return number of occurrences of value """
return 0
6. extend
从右边合并,接受的参数也是一个可迭代对象(deque本身也是一个可迭代对象),会改变原始的值
def extend(self, *args, **kwargs): # real signature unknown
""" Extend the right side of the deque with elements from the iterable """
pass
def extendleft(self, *args, **kwargs): # real signature unknown
""" Extend the left side of the deque with elements from the iterable """
pass
8. index
找打第一个元素的索引值,找不到就抛出ValueError
异常,第一个参数是要找的值,第二、三是可选参数,表示从哪里(索引值)开始找,类似切片,前取后不取
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
D.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" D.insert(index, object) -- insert object before index """
pass
def pop(self, *args, **kwargs): # real signature unknown
""" Remove and return the rightmost element. """
pass
def popleft(self, *args, **kwargs): # real signature unknown
""" Remove and return the leftmost element. """
pass
def remove(self, value): # real signature unknown; restored from __doc__
""" D.remove(value) -- remove first occurrence of value. """
pass
def reverse(self): # real signature unknown; restored from __doc__
""" D.reverse() -- reverse *IN PLACE* """
pass
def rotate(self, *args, **kwargs): # real signature unknown
""" Rotate the deque n steps to the right (default n=1). If n is negative, rotates left. """
pass