2010年12月2日

Python programming tips

一些實用的小技巧,可減少多餘的動作

  1. Swap
  2. //C++和Java的寫法:
    temp = x
    x = y
    y = temp

    #Python建議使用
    x, y= y, x

  3. 讀dict時避免判斷key的存在
  4. d = { 'key': 'value' }
    #一般寫法
    if 'key' in d: print d['key']
    else: print 'not find'

    #建議寫法
    print d.get('key', 'not find')

  5. 尋找最小值和對應位置
  6. s = [ 4,1,8,3 ]
    #一般寫法
    mval, mpos = MAX, 0
    for i in xrange(len(s)):
    if s[i] < mval: mval, mpos = s[i], i

    #建議寫法
    mval, mpos = min([ (s[i], i) for i in xrange(len(s)) ])

  7. 讀取檔案
  8. #一般寫法
    line = ''
    fp = open('text.txt', 'r')
    for line in fp: text += line

    #建議寫法1
    text = string.join([ line for line in open('text.txt')], '']

    #建議寫法2
    text = ''.join([ line for line in open('text.txt')])

    #建議寫法3
    text = file('text.txt').read()

  9. Python使用三元式
  10. #一般寫法,C++會使用?:來處理
    if n >= 0: print 'positive'
    else: print 'negitive'

    #建議寫法1, 但後面式子為None會有問題
    print (n >= 0) and 'positive' or 'negitive'

    #建議寫法2, 解決了None的問題
    print (n >= 0 and ['positive'] or ['negitive])[0]

    #建議寫法3
    print ('negitive', 'positive')[n >= 0]

  11. Dict成員是複雜類別時的初使用
  12. #一般寫法
    if not y in d: d[y] = { }
    d[y][x] = 3

    #建議寫法
    d.setdefault(y, { })[x] = 3

    #如果為list時
    d.setdefault(key, []).append(val)

Python好用的數值計算套件

在別的地方看到的,順便整理一下, Numpy, Scipy, matplotlib是必備套件,三個套件已經有matlab基本的運算功能。

  1. xlrd - Library for developers to extract data from Microsoft Excel spreadsheet files 
  2. RPy2 - A simple Python interface for R
  3. NetworkX -For analyzing network data
  4. SymPy - SymPy contains nearly all of the same functionality (algebraic evaluation, differentiation, expansion, complex numbers, etc.) as SimPy, but is contained in a pure Python distribution.
  5. SimPy - Short for “Simulation in Python”, an object-oriented, process-based discrete-event simulation language, making it a wholesale agent-based modeling environment written entirely in Python.
  6. Boost.Python - This C++ library enables seamless interoperability between C++ and Python
  7. PyMC - PyMC implements the Metropolis-Hastings algorithm as a Python class.
  8. Pycluster - It contains implementations of hierarchical and k-means clustering
  9. NLTK - Natural Language Toolkit is a suite of libraries and programs for symbolic and statistical natural language processing (NLP)