def f(x):
def g(y):
return y + x + 3
return g
nf1 = f(1)
nf2 = f(3)
print(nf1(1))
print(nf2(1))
The previous example returns the following output:
5
7
將列表中元素反轉(zhuǎn)排序肢础,比如下面這樣
>>> x = [1,5,2,3,4]
>>> x.reverse()
>>> x
[4, 3, 2, 5, 1]
列傳字符串:
>>> l2 = ['1','2','3','4','5']
>>> ''.join(l2)
'12345'
字符串反轉(zhuǎn):
方法一嫉戚,使用[::-1]:
s = 'python'
print s[::-1]
方法二梅肤,使用reverse()方法:
l = list(s)
l.reverse()
print ''.join(l)
輸出結(jié)果:
nohtyp
nohtyp
1伴郁。大數(shù)據(jù)量的list身坐,要進(jìn)行局部元素刪除秸脱,盡量避免用del隨機(jī)刪除,非常影響性能部蛇,如果刪除量很大摊唇,不如直接新建list,然后用下面的方法釋放清空舊list搪花。
2遏片。對(duì)于一般性數(shù)據(jù)量超大的list,快速清空釋放內(nèi)存撮竿,可直接用 a = [] 來(lái)釋放吮便。其中a為list。
3幢踏。對(duì)于作為函數(shù)參數(shù)的list髓需,用上面的方法是不行的,因?yàn)楹瘮?shù)執(zhí)行完后房蝉,list長(zhǎng)度是不變的僚匆,但是可以這樣在函數(shù)中釋放一個(gè)參數(shù)list所占內(nèi)存: del a[:],速度很快搭幻,也徹底:)
4.列出當(dāng)前目錄下的所有目錄:
>>> [x for x in os.listdir('.') if os.path.isdir(x)]
['.lein', '.local', '.m2', '.npm', '.ssh', '.Trash', '.vim', 'Applications', 'Desktop', ...]
5.列出所有的.py文件:
>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
['apis.py', 'config.py', 'models.py', 'pymonitor.py', 'test_db.py', 'urls.py', 'wsgiapp.py']