高效實用數據結構

list + filter()  + map()   + reduce()

 

filter(function, sequence):對sequence中的item依次執行function(item),將執行結果爲True的item組成一個List/String/Tuple(取決於sequence的類型)返回:python

>>> 
>>> 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默認初始值爲空或0spa

>>> 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 + 列表推導式

列表推導式書寫形式: code

[表達式 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)若是要在遍歷中刪除某些元素,須要製做副本(切片) [:] ,而默認循環是不建切片的,以下:blog

image

 

(2)enumerate 獲得list的下標和值ip

>>> 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對應位置的值爲字典utf-8

>>> 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是一個無序不重複元素集,基本功能包括關係測試 和 消除重複 元素. 集合對象還支持並、交、差、對稱差等。get

>>> 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}
>>> 
>>>
相關文章
相關標籤/搜索