对于习惯使用于C++的大佬来说, 容器的使用极大的方便了编程的需要,尤其对于参加算法竞赛的同学们,不必再自己去写类函数(当然了,类函数已经明明白白的)。作为python的使用者,开发者也为大家提供了已经打包好的函数库,import 即可。
今天为大家介绍一些python中数据结构的使用。
Queue classification | Feature |
---|---|
queue.Queue | 先进先出 |
queue.LifoQueue | 后进先出 |
queue.PriorityQueue | 优先队列 |
queue.deque | 双端队列 |
Function | Explanation |
---|---|
put | 放数据,Queue.put( )默认有block=True和timeout两个参数。当block=True时,写入是阻塞式的,阻塞时间由timeout确定。当队列q被(其他线程)写满后,这段代码就会阻塞,直至其他线程取走数据。Queue.put()方法加上 block=False 的参数,即可解决这个隐蔽的问题。但要注意,非阻塞方式写队列,当队列满时会抛出 exception Queue.Full 的异常 |
get | 取数据(默认阻塞),Queue.get([block[, timeout]])获取队列,timeout等待时间 |
empty | 如果队列为空,返回True,反之False |
qsize | 显示队列中真实存在的元素长度 |
maxsize | 最大支持的队列长度,使用时无括号 |
join | 实际上意味着等到队列为空,再执行别的操作 |
take_done | 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号 |
full | 如果队列满了,返回True,反之False |
clear | 清空队列 |
import queue
q = queue.Queue(5) #定义一个长度为10 的队列,默认无限长
print(q.maxsize)
for i in range(5):
q.put(i)
while not q.empty():
print(q.get())
---------------------------
10
0
1
2
3
4
import queue
lq = queue.LifoQueue(5) #定义一个长度为10 的队列,默认无限长
for i in range(5):
lq.put(i)
while not q.empty():
print(lq.get())
---------------------------
4
3
2
1
0
import queue
q = queue.PriorityQueue()
q.put((3,'aaaaa'))
q.put((3,'bbbbb'))
q.put((1,'ccccc'))
q.put((3,'ddddd'))
while not q.empty():
print(q.get())
--------------------------
(1, 'ccccc')
(3, 'aaaaa')
(3, 'bbbbb')
(3, 'ddddd')
import queue
dq = queue.deque()
dq.append(1)
dq.append(2)
dq.leftappend(100)
print(q)
print(q.pop())
print(q.popleft())
---------------------------
deque([100, 1, 2])
2
100
Function | Explanation |
---|---|
heappush(heap, x) | 将x压入堆中 |
heappop(heap) | 从堆中弹出最小的元素 |
heapify(heap) | 让列表具备堆特征 |
heapreplace(heap, x) | 弹出最小的元素,并将x压入堆中 |
nlargest(n, iter) | 返回iter中n个最大的元素 |
nsmallest(n, iter) | 返回iter中n个最小的元素 |
import heapq
from random import shuffle
x = list(range(5))
shuffle(x)
heap = []
for i in x:
heappush(heap,i)
print(heap)
heappush(heap,-10)
print(heap)
print(heappop(heap))
print(heapreplace(heap, 9))
print(heap)
-------------------
[0, 2, 1, 3, 4]
[-10, 2, 0, 3, 4, 1]
-10
0
[1, 2, 3, 4, 9]
collections模块实现一些特定的数据类型,可以替代Python中常用的内置数据类型如dict, list, set, tuple,简单说就是对基本数据类型做了更上一层的处理。
import collections
print(dir(collections))
-------------------------------------
['AsyncGenerator', 'AsyncIterable', 'AsyncIterator', 'Awaitable', 'ByteString', 'Callable', 'ChainMap', 'Collection', 'Container', 'Coroutine', 'Counter', 'Generator', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'OrderedDict', 'Reversible', 'Sequence', 'Set', 'Sized', 'UserDict', 'UserList', 'UserString', 'ValuesView', '_Link', '_OrderedDictItemsView', '_OrderedDictKeysView', '_OrderedDictValuesView', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_chain', '_class_template', '_collections_abc', '_count_elements', '_eq', '_field_template', '_heapq', '_iskeyword', '_itemgetter', '_proxy', '_recursive_repr', '_repeat', '_repr_template', '_starmap', '_sys', 'abc', 'defaultdict', 'deque', 'namedtuple']