python list转map_Python 进阶之术 Map Filter Reduce

本文字数:763 字 || 

阅读时间:3

分钟   "

Map 

Map 会将⼀个函数映射到⼀个输⼊列表的所有元素上。

这是它的规范:规范 map(function_to_apply, list_of_inputs) ⼤多数时候,

我们要把列表中所有元素⼀个个地传递给⼀个函数,并收集输出。

⽐⽅说:

items = [1, 2, 3, 4, 5]squared = []for i in items:    squared.append(i**2)

Map 可以让我们⽤⼀种简单⽽漂亮得多的⽅式来实现。

就是这样:

items = [1, 2, 3, 4, 5]squared = list(map(lambda x: x**2, items))

⼤多数时候,我们使⽤匿名函数(lambdas)来配合 map

不仅 ⽤于⼀列表的输⼊, 我们甚⾄可以⽤于⼀列表的函数

def multiply(x):    return (x*x)def add(x):    return (x+x)funcs = [multiply, add]for i in range(5):    value = map(lambda x: x(i), funcs)print(list(value))# 上⾯print时,加了list转换,是为了python2/3的兼容性# 在python2中map直接返回列表,但在python3中返回迭代器# 因此为了兼容python3, 需要list转换⼀下# Output:# [0, 0]# [1, 2]# [4, 4]# [9, 6]# [16, 8]

Filter

顾名思义,filter过滤列表中的元素,并且返回⼀个由所有符合要求的元素所

构成的列表,符合要求即函数映射到该元素时返回值为True. 

这⾥是⼀个简短的例⼦:

number_list = range(-5, 5)less_than_zero = filter(lambda x: x < 0, number_list)print(list(less_than_zero))# 上⾯print时,加了list转换,是为了python2/3的兼容性# 在python2中filter直接返回列表,但在python3中返回迭代器# 因此为了兼容python3, 需要list转换⼀下# Output: [-5, -4, -3, -2, -1]

这个 filter 类似于⼀个 for 循环,但它是⼀个内置函数,并且更快。

注意:如果 map 和 filter 对你来说看起来并不优雅的话,那么你可以试试列 表/字典/元组推导式。⼤部分情况下推导式的可读性更好     

Reduce 

当需要对⼀个列表进⾏⼀些计算并返回结果时,Reduce 是⾮常有⽤的函数。

举个例 ⼦,当你需要计算⼀个整数列表的乘积时。

通常在 Python 中你可能会使⽤基本的 for 循环来完成这个任务。

现在我们来试试 reduce:

from functools import reduceproduct = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )# Output: 24

bdf1d91123c90e8fa5f9fddef872a9d2.png

推荐阅读

疫情查询服务上线

Python [**kwargs 的⽤法]

Python ⽣成器(Generators)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值