1.列表git
list[起始:結尾:增量] express
值傳遞與地址傳遞函數
a = [2,1] b = a #地址傳遞 b = a[:] 值傳遞 a.sort() print a #[1,2] print b #[1,2]
2.排序sort()spa
d = {1:'s',2:'a'} print sorted(d.items(), key = lambda x:x[1])/*按list元素的第2個元素排序*/
#sorted(iterable, cmp=None/*函數*/, key=None/*函數*/, reverse=False) ll = ['aaa','a','aa'] ll.sort( key = len )#key值是總要調用的函數 print ll
print sorted("Sorting1234", key=lambda x: (x.isdigit(), x.isdigit() and int(x) % 2 == 0, x.isupper(), x.islower(), x))#['g', 'i', 'n', 'o', 'r', 't', 'S', '1', '3', '2', '4']
3.匿名函數lambdacode
#lambda 形參:返回值 lambda x:x+1
4....for ... in ... if ...blog
a = [1,2,3] print [i**2 for i in a if i>0]
5.filter map reduce排序
filter(function, sequence):對sequence中的item依次執行function(item),將執行結果爲True的item組成一個List/String/Tuple(取決於sequence的類型)返回generator
print filter( lambda x:x!='a', 'abcd' ) #'bcd'
map(function, sequence) :對sequence中的item依次執行function(item),見執行結果組成一個List返回:it
print map( lambda x:x+1, range(3) ) #[1,2,3]
reduce(function, sequence, starting_value):對sequence中的item順序迭代調用function,若是有starting_value,還能夠做爲初始值調用,例如能夠用來對List求和io
print reduce( lambda x,y:x+y, range(3), 10 ) #13
6.生成器 generator
a = (x * x for x in range(3)) for i in a:print i
7. range(起始,結尾,增量)
print range(1,9,2) #[1, 3, 5, 7]
8. ...?...:...
(1) variable = a if expression else b #徹底等價 (2) variable = (expression and [b] or [c])[0] (3) variable = expression and b or c #不徹底等價,b 不能爲False
f