標籤:python lambda utf-8 c++
# -*- coding: utf-8 -*- #utf-8支援中文編碼 words=['cat','dog','chicken']for w in words[:]: #words[:]複製了原本的list words.insert(0, w)print words a = range(0,10,4)print aargs=[3,10,3]print range(*args)#[3, 6, 9]#我們還可以把range的argument儲存在list或tuple中 def f(a, L=[]): L.append(a) return L print f(1)print f(2)print f(3)[1][1, 2][1, 2, 3]'''函數形參的預設值只初始化第一次,即這是靜態變數'''def a(a=0): a=a+1 print a a()a()'''但是這裡面顯示的只是1因為python 中的 mutable object 是list,dictionary,instances'''def aa(*args,**keys): for a in args: print a for d in keys: print d, ':', keys[d] aa(1,2,3,a=1,b=2,c=3)#類似cpp的*arg 加*表示它會接受arbitrary個arg,**表示接受arbitrary個dict def parrot(voltage, state='a stiff', action='voom'): print "-- This parrot wouldn't", action, print "if you put", voltage, "volts through it.", print "E's", state, "!" d = {"state": "bleedin' demised","voltage": "four million", "action": "VOOM"}parrot(**d)#-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !#我們可以把參數儲存在字典中,如果key與argument對應的化. f=lambda a,b,c:a*b+cprint f(2,3,5)#輸入前面的argument, 返回後面的值 #字典樹user={}user['cs']={}user[2]={}user['cs']['bo']=1user['cs']['co']=2user[2][1]=1print user.keys()print user.values()print user['cs'].values()print user['cs'].keys()
c++轉python知識小記之一