python standard library

Standard Library 中有用的函数
https://docs.python.org/2/library/index.html
http://python-reference.readthedocs.io/en/latest/index.html

binO: int -> binary
位运算的时候有用。
相关位运算有用的打包函数有:

  • divmod()
  • binary->int   int([binary string], 2)
  • ... 以后再补充

Data structure:
这是可以用于threading的queue. 是否thread safe有待考证。。
https://docs.python.org/2/library/queue.html
Example of how to wait for enqueued tasks to be completed:
def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

q = Queue()
for i in range(num_worker_threads):
     t = Thread(target=worker)
     t.daemon = True
     t.start()

for item in source():
    q.put(item)

q.join()       # block until all tasks are done


http://book.pythontips.com/en/latest/map_filter.html

http://zh-google-styleguide.readthedocs.io/en/latest/contents/

http://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Random函数
https://docs.python.org/2/library/random.html

High Performance Data Structures
https://docs.python.org/2/library/collections.html#collections.defaultdict

defaultdict:
  • 特点: 
    • 不需要check key是否存在,当key不存在的时候自动调用构造函数新建一个default value的值。前提是,在见defalutdict的时候需要specify value的type, specify的type需要有一个default constructor
  • 常用的成员函数:


deque: double

heapq: max heap, priority queue

牛逼的counter函数
import collections

words = set(["I", "am", "cd"])
bits = "I I am cd"
res = collections.Counter(b for b in bits.split() if b in words)
print bits
print res["I"]

Comments

Popular posts from this blog

Sudoku

Longest prefix matching - trie

Climbing Stairs