您的位置:首页 > 编程语言 > Python开发

Python中filter、lambda、map、reduce的使用

2016-07-20 11:55 796 查看
filter

官方释义:

>>> help(filter)
Help on built-in function filter in module __builtin__:

filter(...)
filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If
function is None, return the items that are true.  If sequence is a tuple
or string, return the same type, else return a list.


返回类型是列表、字符串或者元组,如果序列本身是字符串或者元组,返回的也是字符串或者元组,其他都返回列表。返回序列中逐个应用函数(或者没有函数)之后结果为true的所有元素。

例:

>>> def foo(x):
...     return x % 2 != 0 and x % 3 != 0
...
>>> filter(foo, range(2,10))
[5, 7]

>>> filter(None, range(2, 10))

>>> numbers = range(-5 , 5)
>>> numbers
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
>>> filter(lambda x : x > 0, numbers)
[1, 2, 3, 4]

>>> filter(lambda c : c != 'i', 'fajououqiyi')
'fajououqy'


filter函数使用方法小结

filter的第一个参数是函数名或者None,一定要有,不然会报错,如果使用函数只要写函数的名称即可,不需要括号或者参数

filter的第二个参数是要操作的序列

lambda

简洁的计算函数。

例:

>>> numbers = range(10)
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> num_new = []
>>> lam = lambda x : x + 3
>>> for i in numbers:
...     num_new.append(lam(i))
...
>>> num_new
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

>>> g = lambda x, y : x + y
>>> g(3, 4)
7

>>> (lambda x : x**2)(4)
16


lambda函数使用方法小结

在lambda后面直接跟变量

变量后面是冒号

冒号后面是表达式,表达式计算结果就是本函数的返回值

map

官方释义:

>>> help(map)
Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s).  If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length.  If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).


返回的是序列应用函数之后的结果,返回结果是一个列表

如果函数不为None,并且只有一个序列参数,一次将序列中的元素应用函数(实际上就是for循环)

如果函数不为None,并且有多个序列参数,就将序列参数中对应的元素构成元组再应用函数

如果函数为None,则返回序列本身(如果有多个序列参数,序列中的每个元素就是元组)

例:

>>> numbers = range(10)
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> map(lambda x : x + 3, numbers)
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

>>> map(None, numbers)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> map(None, numbers, numbers)
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]

>>> map(None, numbers, numbers, numbers)
[(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5), (6, 6, 6), (7, 7, 7), (8, 8, 8), (9, 9, 9)]

>>> items = range(1, 6)
>>> map(lambda x : x**2, items)
[1, 4, 9, 16, 25]

>>> items = range(1, 6)
>>> items1 = range(6, 11)
>>> map(lambda x , y : x + y, items, items1)
[7, 9, 11, 13, 15]


reduce

官方释义:

>>> help(reduce)
Help on built-in function reduce in module __builtin__:

reduce(...)
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.


依次将序列中元素应用函数

最后返回的是一个元素而不是序列

例:

>>> reduce(lambda x, y : x * y, range(1, 6))
120


有两个 list,a = [3,9,8,5,2],b=[1,4,9,2,6],计算:a[0]b[0]+a[1]b[1]+…的结果。

>>> a = [3, 9, 8, 5, 2]
>>> b = [1, 4, 9, 2, 6]
>>> sum(x * y for x, y in zip(a, b))
133

>>> reduce(lambda x , y : x + y, map(lambda x, y : x * y, a, b))
133
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python lambda filter