- '''
- Python --version :Python 2.7.11
- Quote : https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
- Add by camel97 2017-04
- '''
- 1.filter()
-
#
filter(function, sequence)returns a sequence consisting of those items from the sequence for whichfunction(item)is true. If sequence is astr,unicodeortuple, the result will be of the same type; otherwise, it is always alist. For example, to compute a sequence of numbers divisible by 3 or 5: - #你可以把 filter 當成一個過濾器,用來選擇原來 list 中滿足特定條件的 value。它有兩個參數。第一個參數是一個返回 bool 類型的函數,第二個參數可以是一個 list 。 filter()會返回一個新的 list ,這個新的 list 中的值同時滿足這樣兩個條件。第一,他們屬於傳給 filter() 的 list 的值。第二,它們作為參數傳給 function 函數時,函數會返回 True。
1 def f(x): 2 return x % 3 == 0 or x % 5 == 0 3 print filter(f, range(2, 25)) 4 5 ==>[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]
顯然,filter() 篩選出了原來的 list ( range(2,25) )中能被 3 整除或者能被 5 整除的數
2.map()
#map(function, sequence) calls function(item) for each of the sequence’s items and returns a list of the return values. For example, to compute some cubes:
#map 函數可以把 list 中的每一個 value 傳給函數,並且將每一次函數返回的結果合到一起生成一個新的 list
#它可以被用來這樣操作:首先定義一個對某一個參數執行算數運算的函數,然后通過 map 函數來對 list 中的每一個 value 進行函數定義過的算數運算
1 print map(lambda x:x*x*x, range(1, 11)) 2 3 ==>[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
這個 demo 用來對 list(range(1,11))中的每一個 value 執行 f(x) = x*x*x 操作
#More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another).
#map 函數可以傳不止一個 list 。但同時函數也必須執行對不止一個參數的算數運算。list 的個數和函數的參數個數應當是對應的。
1 seq = range(8) 2 print map(lambda x,y : x+y, seq, seq) 3 4 ==>[0, 2, 4, 6, 8, 10, 12, 14]
這個 demo 顯示了如何對兩個 list 中對應的 value 實行求和操作
1 seq = range(8) 2 seqcopy = range(7) 3 print map(lambda x,y : x+y, seq, seqcopy) 4 5 ==>TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
如圖所示,當兩個 list 中的 value 個數不相等時, 程序會報一個 TypeError 錯誤。因為一個 NoneType 不能和任何類型的數據進行運算
3.reduce()
#reduce(function, sequence) returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10:
#reduce() 函數返回一個值而不是一個 list 。首先你需要定義一個需要對兩個參數進行算數運算的函數。reduce()函數首先會對 list 中的第一個 value 和第二個 value 進行這個算數運算,這會得到一個 result 。之后它會對這個 result 和 list 中的第三個 value 進行這個算數運算得到一個新的 result 。它會這樣一直進行下去直到這個 list 中的所有 value 都參與了運算並且返回最后一次的 result。
1 print reduce(lambda x,y : x+y ,range(11)) 2 3 ==>55
如圖完成了對 range(11) 中的10個數的求和操作
#If there’s only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised.
#如果 list 中只有一個數據,那么 reduce() 將不會進行任何運算而是直接返回這個數。如果 list 為空,reduce() 將會報一個異常 TypeError
1 print reduce(lambda x,y : x+y,[1]) 2 ==>1 3 4 print reduce(lambda x,y : x+y,[]) 5 ==>TypeError: reduce() of empty sequence with no initial value
#A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on.
#reduce() 可以添加第三個參數。這個參數相當於一個初始值。當給了第三個參數之后,對於一個空的 list ,reduce() 會返回這個初始值而不是拋出一個異常(和上一種情況做比較)。當給了第三個參數之后,reduce() 會首先計算這個初始值和第一個 value 的算數運算結果,其余步驟均相同
1 print reduce(lambda x,y : x+y , range(11) , 11) 2 ==>66 3 4 print reduce(lambda x,y : x+y,[1] , 2) 5 ==>3 6 7 print reduce(lambda x,y : x+y , [] , 0) 8 ==>0
注意,雖然我們說這三個函數在 list 中是非常有用的,但是它們並不是只能用在 list 這一種數據類型中。
當然。傳入的數據類型需要能夠被迭代。
