發表文章

目前顯示的是 2014的文章

[Python] docopt

今天因為要在程式加上 optionParser 發現 python v2.7 不支援了 只好找尋其他 library使用, 我也知道其實早就有 argparser 可是就覺得他好難用, 當初要寫他文件就寫得很零碎 今天 google 了一番, 發覺有個東西叫 docopt , 好像好好用阿, 就來試用一下 一用之下, 真是令人清爽, 而且強迫你建立好習慣, 是個好 library Official site:  http://docopt.org/ pip:  https://pypi.python.org/pypi/docopt

[Python] list flatten

最近剛好碰到需要做 list flatten 的行為 網路上查了一下, 順便做了一點不專業的比較 故事是這樣的 今天我有一個 list 中的 list tmp_list = [[1, 2, 3, 4], [5, 6, 7, 8]] 我今天想要做的事情就是把他平扁化, 也就是變成 [1, 2, 3, 4, 5, 6, 7, 8] 查到有兩種作法 1. 利用 itertools import itertools list(itertools.chain.from_iterable(tmp_list)) # [1, 2, 3, 4, 5, 6, 7, 8] 正當我覺的還要多 import module 不太滿意的時候, 看到了第二種作法 2. 利用 sum sum(tmp_list, []) # [1, 2, 3, 4, 5, 6, 7, 8] 頓時讓我覺的好聰明阿, 不過一般人來講應該會寫的第三種 3. 利用 list comprehensive [item for sub_list in tmp_list for item in sub_list] # [1, 2, 3, 4, 5, 6, 7, 8] 那順便做一個大的 list 來試試看哪個比較威XD import itertools def make_list(): return [range(100) for i in range(1000)] @profile def merge_iter(tmp_list): return list(itertools.chain.from_iterable(tmp_list)) @profile def merge_sum(tmp_list): return sum(tmp_list, []) @profile def merge_comph(tmp_list): return [item for sub_list in tmp_list for item in sub_list] if __name__ == '__main__': tmp_list = make_list() merge_iter(tmp_list) m

[Python] Python 入門 - Coursera

好像很久沒有更新網誌(汗) 最近也有一門 Coursera 的 Python 課程開了 有興趣從基礎開始學的的人可以去看看 是美國 Rice 大學開的課程: https://class.coursera.org/interactivepython-004 裡面使用線上的 python 來教學 : http://www.codeskulptor.org/ 有興趣得人可以去看看, 完全不會 Python 的人很適合去學