list + filter() + map() + reduce()
filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回:
>>> >>> def f(x): return x % 2 != 0 and x % 3 != 0 ... >>> y=filter(f, range(2, 25)) >>> print y [5, 7, 11, 13, 17, 19, 23] >>>
map(function, sequence) :对sequence中的item依次执行function(item),见执行结果组成一个List返回:
>>> def cube(x): return x*x*x ... >>> y=map(cube, range(1, 11)) >>> >>> print y [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def cube(x) : return x + x ... >>> z=map(cube , "abcde") >>> >>> print z ['aa', 'bb', 'cc', 'dd', 'ee'] >>> >>> >>> >>> def add(x, y): return x+y ... >>> z=map(add, range(8), range(8)) >>> print z [0, 2, 4, 6, 8, 10, 12, 14]
reduce(function, sequence, starting_value):对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用,function返回结果和list中的遍历值按function的方式结合。function默认初始值为空或0
>>> arr=['a','b','c','d'] >>> def add(x,y): return x + y ... >>> z=reduce(add,arr) >>> z 'abcd' >>> z=reduce(add,arr,'00') >>> z '00abcd' >>>
list + 列表推导式
列表推导式书写形式:
[表达式 for 变量 in 列表] 或者 [表达式 for 变量 in 列表 if 条件]
>>> x=[str(round(math.pi,i)) for i in range(1,10)] >>> x ['3.1', '3.14', '3.142', '3.1416', '3.14159', '3.141593', '3.1415927', '3.14159265', '3.141592654'] >>> >>> >>> t=[(x,y) for x in [1,2,3] for y in[3,1,4] if x!=y] >>> t [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] >>> x=[(x,x**2) for x in range(6)] >>> x [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)] >>> arr = [[1, 2, 3], [4, 5, 6], [7,8, 9], [10, 11, 12]] >>> y=[[r[col] for r in arr] for col in range(len(arr[0]))] >>> y [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]] >>> y=zip(*arr) >>> y [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] # zip: 一系列可迭代对象作为入参(组成list),将每个 list中对应相同位置的元素打包成tuple,多个tuple组成list返回 >>> zz=zip(*arr) >>> zz [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] >>> uu=map(list,zip(*arr)) >>> uu [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
#!/usr/bin/python # -*- coding: utf-8 -*- li = [1,2,3,4,5,6,7,8,9] print [x**2 for x in li] print [x**2 for x in li if x>5] print dict([(x,x*10) for x in li]) print [ (x, y) for x in range(10) if x % 2 if x > 3 for y in range(10) if y > 7 if y != 8 ] vec=[2,4,6] vec2=[4,3,-9] sq = [vec[i]+vec2[i] for i in range(len(vec))] print sq print [x*y for x in [1,2,3] for y in [1,2,3]] testList = [1,2,3,4] def mul2(x): return x*2 print [mul2(i) for i in testList]
list应用
(1)如果要在遍历中删除某些元素,需要制作副本(切片) [:] ,而默认循环是不建切片的,如下:
(2)enumerate 得到list的下标和值
>>> x=[1,2,3,4,5,6] >>> y=[(idx,val) for idx,val in enumerate(x) ] >>> print y [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
(3) zip 合成2个list对应位置的值为字典
>>> x=['a','b','c','d'] >>> y=[1,2,3,4] >>> d={ k:v for k,v in zip(x,y) } >>> print d {'a': 1, 'c': 3, 'b': 2, 'd': 4} >>>
tuple应用
tuple不能被修改,只能和其它tuple联合
>>> x=(1,2) >>> y=('a','b','c') >>> >>> x[0]=3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> >>> >>> z=x+y >>> z (1, 2, 'a', 'b', 'c')
set应用
python的set是一个无序不重复元素集,基本功能包括关系测试 和 消除重复 元素. 集合对象还支持并、交、差、对称差等。
>>> x = set("jihite") >>> y = set(['d', 'i', 'm', 'i', 't', 'e']) >>> x #把字符串转化为set,去重了 set(['i', 'h', 'j', 'e', 't']) >>> y set(['i', 'e', 'm', 'd', 't']) >>> x & y #交 set(['i', 'e', 't']) >>> x | y #并 set(['e', 'd', 'i', 'h', 'j', 'm', 't']) >>> x - y #差 set(['h', 'j']) >>> y - x set(['m', 'd']) >>> x ^ y #对称差:x和y的交集减去并集 set(['d', 'h', 'j', 'm'])
>>> x='abc43523rqweadsfabcwekrabc234123' >>> >>> y={i for i in x if i not in 'abc'} >>> >>> y set(['1', 'e', 'd', 'f', 'k', 's', 'q', '3', 'r', '5', '4', 'w', '2']) >>> >>> z=set( i for i in x if i not in 'abc') >>> z set(['1', 'e', 'd', 'f', 'k', 's', 'q', '3', 'r', '5', '4', 'w', '2']) >>> >>> l=[1,2,2,3,3,35,123,4,4,6,7,8] >>> out=list(set(l)) >>> out [1, 2, 3, 4, 6, 7, 8, 35, 123]
dict应用
key:value序列,key为不可变的任意类型,无序
>>> x=[('a',1),('b',2),('c',3)] >>> >>> y=dict(set(x)) >>> y {'a': 1, 'c': 3, 'b': 2} >>> >>> z={x:x**2 for x in range(20)} >>> z {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361} >>> >>>