Python Common Library

Some common Python library.

Collections

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Priority Queue, put value, and get the min value
import queue
pq = queue.PriorityQueue()
pq.put(2)
pq.put(4)
num = pq.get()

# Heap, push value, and get the min value
import heapq
nums = []
heapq.heappush(nums, 2)
heapq.heappush(nums, 4)
num = heapq.heappop(nums)

# Dictionary
dic = {}
dic = dict()
dic = collections.defaultdict()
dic = collections.defaultdict(list) # value is a list
dic = colelctions.Counter([1, 1, 2, 3, 4]) # generate a {num: frequency} pair

# Set
se = set()

# Queue, stack, double-queue
a = []
a.append(0)
a.append(1)
a.pop(0)
a.pop()