[python] python 中map、reduce和filter 浅析

本文深入讲解了Python中的三个内置函数:map(), reduce() 和 filter() 的使用方法与应用场景,包括函数参数、返回值及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0x00      map()

如果你想先了解下什么是map()和reduce(),可以参考MapReduce:Simplified Data Processing On Large Clusters这篇文章。

map() 和 reduce() 都是python 内置函数 

可以看到在python文档中对map() 、reduce()的描述:

https://docs.python.org/3.4/library/functions.html#map


map ( functioniterable... )

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

可以看到,map()函数接受两个参数,function和iterable , function是函数对象,多数时候是自定义的函数,第二个参数iterable是一个可迭代对象,而map()函数的功能就是将函数function作用在可迭代对象iterable中的每一个元素上,并生成一个新的列表,也就是说,我们也可以对tuple等不可改变对象进行map()操作。现在举个map()的例子,假设我们有一个已经排好序的列表 [1,2,3,4,5,6,7], 现在我需要将他们每一个数都乘10再加1,那么用map()函数来实现就是这样:

>>> a = [1, 3, 4, 7, 9, 18]
>>> b = map( lambda x: x*10+1, a)
>>> b
[11, 31, 41, 71, 91, 181]

0x01     reduce()


关于reduce描述在文档中也有描述,不过在python3.x中,reduce()函数被放置在functools模块中,所以使用前需要导入

from functools import reduce

首先来看看文档中关于reduce()函数的描述
functools. reduce ( functioniterable [initializer ] )

Apply function of two arguments cumulatively to the items of 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). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

Roughly equivalent to:

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value
原理上面说的很清楚来,reduce好像是在读书一样,每次读到的新东西都会和你所有学过的东西进行融合,比如:
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 就代表着 ((((1+2)+3)+4)+5)
reduce()函数接受三个参数其中最后一个参数为缺省参数,function为作用函数,该函数参数必须是两个。iterable是一个可迭代对象。initializer是默认元素,如果不传入initializer,会默认为None。要注意的是,在函数reduce()执行时,会先检查initializer添加到可迭代列表的最前面(当然如果是None就什么都没有发生),也就是说,在上面加法的例子中,如果传出的列表是 [ 1 , ] ,并且将缺省参数initializer指定为2,那么运算过程就是(2+1)。还有一种情况是列表中只有一个元素,缺省参数也是None,那么reduce函数返回的就是列表中那个唯一的元素,不做任何运算(因为没办法算)。当然如果列表为空,initializer参数也空,就会报错的。代码测试情况:
>>> list1 = [1, 2, 3, 4, 5 ]
>>> list2 = [34, ]
>>> list3 = []
>>> def func(x, y):
...     return x * y
...
>>> reduce(func, list1)
120
>>> reduce(func, list1, 6)
720
>>> reduce(func, list2)
34
>>> reduce(func, list2, 2)
68
>>> reduce(func, list3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value
(ps:上面例子func为乘法函数。)

0x02     filter()



filter ( functioniterable )

Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) 

if function is not None and (item for item in iterable if item) if function is None

See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.

filter翻译过来是过滤器,和map()类似,filter()函数也接受一个函数和一个可迭代序列,和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。


资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距填充,对 html body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值