Python中map(),filter(),reduce()及sum()的使用方法比较

看《python学习手册》看到列表那一节,发现有提到map函数和filter函数,作者并没有作细讲。自己总结了下。

以下例子都以列表作为示例

1.map函数

map函数有两种使用方法:

(1) 可以做判断,判断列表里的元素是否符合lambda给的函数映射。返回值为True和False。例如:

L1 = list(map(lambda x:x%2 ==0,range(1,15)))
print('L1 is : {} '.format(L1))
#L1 is : [False, True, False, True, False, True, False, True, False, True, False, True, False, True] 

(2) 按lambda函数式计算,返回计算后的结果,例如:

L3 = list(map(lambda x:x*x,range(1,15)))
print('L3 is : {}'.format(L3))
#L3 is : [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196]

2.filter函数

filter函数用法和其名称相符,过滤出符合lambda函数式的元素,可以和L1的map做比较,例如:
L2 = list(filter(lambda x:x%2 ==0,range(1,15)))
print('L2 is : {}'.format(L2))
#L2 is : [2, 4, 6, 8, 10, 12, 14]

3.reduce函数

reduce函数是将列表中的元素从左到右将列表中的元素按lambda函数式计算一次,返回一个最终结果。例如:

L4 = reduce(lambda x,y: x+y,[1,2,3,4])
print('L4 is : {}'.format(L4))
#L4 is : 10

计算的顺序为(((1+2)+3)+4)=10

4.sum函数

sum函数没别的,就是列表内元素累加,返回最终结果。由于python列表求和比较常用,所以专门有了这个函数。例:

L5 = sum(range(1,20))
print('L5 is : {}'.format(L5))
#L5 is : 190

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
`map`, `filter`, and `reduce` are three higher-order functions commonly used in functional programming languages like JavaScript, Python, and others. These functions operate on lists, arrays, or other iterable objects and allow for concise and expressive manipulation of data. Here's a brief explanation of each function: 1. `map`: The `map` function applies a given function to each element in a list and returns a new list with the transformed values. It takes in two arguments: the function to apply and the list to operate on. The resulting list will have the same length as the original list. Example: ```python # Double each element in the list using map numbers = [1, 2, 3, 4, 5] doubled_numbers = list(map(lambda x: x * 2, numbers)) print(doubled_numbers) # Output: [2, 4, 6, 8, 10] ``` 2. `filter`: The `filter` function creates a new list by selecting elements from an existing list that satisfy a given condition. It takes in two arguments: the condition (often expressed as a lambda function) and the list to filter. Only the elements that evaluate to `True` for the condition will be included in the resulting list. Example: ```python # Filter out even numbers from the list using filter numbers = [1, 2, 3, 4, 5] filtered_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(filtered_numbers) # Output: [2, 4] ``` 3. `reduce`: The `reduce` function applies a given function to a sequence of elements, reducing them to a single value. It takes in two arguments: the function to apply and the sequence to reduce. The function must take two arguments and return a single value. The result of each reduction is passed as the first argument to the next reduction until a single value is obtained. Example: ```python from functools import reduce # Compute the sum of all elements in the list using reduce numbers = [1, 2, 3, 4, 5] sum_of_numbers = reduce(lambda x, y: x + y, numbers) print(sum_of_numbers) # Output: 15 ``` In summary, `map` allows for transforming each element in a list, `filter` allows for selecting elements based on a condition, and `reduce` allows for combining elements into a single value. These functions provide powerful tools for manipulating and processing data in a functional programming paradigm.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值