一. deque(双端队列)
1. 使用 deque(maxlen=N)会新建一个固定大小的队列。当新的元素加入并且这个队列已满的时候,最老的元素会自动被移除掉
>>> from collections import deque >>> q = deque(maxlen=3) >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3], maxlen=3) >>> q.append(4) >>> q deque([2, 3, 4], maxlen=3) >>> q.append(5) >>> q deque([3, 4, 5], maxlen=3)
2. 如果你不设置最大队列大小,那么就会得到一个无限大小队列,你可以在队列的两端执行添加和弹出元素的操作
>>> q = deque() >>> q.append(1) >>> q.append(2) >>> q.append(3) >>> q deque([1, 2, 3]) >>> q.appendleft(4) >>> q deque([4, 1, 2, 3]) >>> q.pop() 3 >>> q deque([4, 1, 2]) >>> q.popleft() 4
优点:使用deque在两端插入或删除元素时间复杂度都是 O(1) ,而在列表的开头插入或删除元素的时间复杂度为 O(N) 。
二. headq模块
扫描二维码关注公众号,回复:
2369115 查看本文章
1. heapq 模块有两个函数:nlargest() 和 nsmallest() 可用来查找最大或最小的N个元素
import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, nums)) # Prints [42, 37, 23] print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]
2. 两个函数都能接受一个关键字参数key,用于更复杂的数据结构中
import heapq portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] cheap = heapq.nsmallest(2, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(2, portfolio, key=lambda s: s['price'])
上面代码在对每个元素进行对比的时候,会以 price 的值进行比较,,如下
>>> expensive [{'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'ACME', 'shares': 75, 'price': 115.65}]
3. 使用heapq.heappop来弹出最小的一个元素,效率高
>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] >>> import heapq >>> heapq.heappop(nums) -4 >>> heapq.heappop(nums) 1 >>> heapq.heappop(nums) 2
heappop方法会先将第一个元素弹出来,然后用下一个最小的元素来取代被弹出元素 (这种操作时间复杂度仅仅是 O(log N),N 是堆大小)
4. nlargest(), max()以及排序切片的使用场景
1) 当要查找的元素个数相对比较小的时候,函数 nlargest() 和 nsmallest() 是很合适的。
2) 如果你仅仅想查找唯一的最小或最大 (N=1) 的元素的话,那么使用 min() 和max() 函数会更快些。
3) 如果 N 的大小和集合大小接近的时候,通常先排序这个集合然后再使用切片操作会更快点 ( sorted(items)[:N] 或者是 sorted(items)[-N:] )。
5. 使用heapq来实现一个优先级队列,并且在这个队列上每次用pop操作都返回一个最高优先级的元素
先写一个PriorityQueue类
import heapq class PriorityQueue: def __init__(self): self._queue = [] self._index = 0 def push(self, item, priority): heapq.heappush(self._queue, (-priority, self._index, item)) self._index += 1 def pop(self): return heapq.heappop(self._queue)[-1]
一个使用上面PriortyQueeu类的例子
>>> class Item: ... def __init__(self, name): ... self.name = name ... def __repr__(self): ... return 'Item({!r})'.format(self.name) # '!r' 用于在format函数中,表示应用repr函数表示。 ... >>> Item('foo') Item('foo') >>> q = PriorityQueue() >>> q.push(Item('foo'), 1) >>> q.push(Item('bar'), 5) >>> q.push(Item('spam'), 4) >>> q.push(Item('grok'), 1) >>> q.pop() Item('bar') >>> q.pop() Item('spam') >>> q.pop() Item('foo') >>> q.pop() Item('grok')
说明:
1. 函数__repr__()是用于显示的,如果不定义这个函数,Item('foo')的返回结果类似<__main__.Item object at 0x7fa8e0edceb8>