目录
1. 集合和多重集合
- 集合的元素是可哈希的
- frozenset:不可变集合
- collections.Counter():多重集合
>>> from collections import Counter
>>> inventory = Counter()
>>> loot = {'sword': 1, 'bread': 3}
>>> inventory.update(loot)
>>> inventory
Counter({'bread':3, 'sword': 1})
>>> more_loot = {'sword': 1, 'apple': 1}
>>> inventory.update(more_loot)
>>> inventory
Counter({'bread':3, 'sword': 2, 'apple': 1})
# 唯一元素的个数
>>> len(inventory)
3
# 元素总数
>>> sum(inventory.values())
6
2. 栈
- list可以通过append和pop()实现栈
- 双端队列collections.deque通过append()和pop()实现栈
- queue.LifoQueue:提供了并发锁语义的数据结构
3. 队列
- list可以通过append和pop(0)实现队列
- 双端队列collections.deque通过append()和popleft()实现栈
- queue.Queue:提供了并发锁语义的数据结构
4. 优先队列
- list可以实现优先队列:bisect.insort找到插入位置后插入或者向list添加元素后排序
- heapq:默认是小根堆
- queue.PriorityQueue:提供了内置的同步操作,可以执行并发读写