Python中reduce()函数的用法详细解读

 更新时间:2023年08月21日 09:50:52   作者:IT之一小佬  
这篇文章主要介绍了Python中reduce()函数的用法详细解读,reduce函数是通过函数对迭代器对象中的元素进行遍历操作,但需要注意的是 reduce 函数返回的是计算的结果,而 map/filter 返回的是作用后的迭代器对象,需要的朋友可以参考下

Python中的reduce()函数

reduce()源码:

def reduce(function, sequence, initial=None): # real signature unknown; restored from __doc__
    """
    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.
    """
    pass

从上述可以看到,reduce()有三个参数,第一个是函数function,第二个是序列sequence,第三个是initial,为初始值,默认为None

reduce(func,lst),其中func必须至少有两个参数。每次func计算的结果继续和序列的下⼀个元素做累积计算。

注意:reduce()传⼊的参数func必须至少接收2个参数。

需求:计算 list1 序列中各个数字的累加和。

示例代码1:

import functools
list1 = [1, 2, 3, 4, 5]
#  方法一
def func(a, b):
    return a + b
result = functools.reduce(func, list1)
print(result)  # 15
# 方法二
result2 = functools.reduce(lambda x, y: x + y, list1)
print(result2)

运行结果:

示例代码2:

import functools
list1 = [1, 2, 3, 4, 5]
#  方法一
def func(a, b):
    return a * b
result = functools.reduce(func, list1)
print(result)  # 15
# 方法二
result2 = functools.reduce(lambda x, y: x * y, list1)
print(result2)

运行结果:

示例代码3:

import functools
list1 = [1, 2, 3, 4, 5]
list2 = [1, 1, 1, 1, 1]
list3 = [0, 0, 0, 0, 0]
list4 = [0, 0, 0, 0, 1]
result1 = functools.reduce(lambda x, y: x & y, list1)
result2 = functools.reduce(lambda x, y: x & y, list2)
result3 = functools.reduce(lambda x, y: x | y, list3)
result4 = functools.reduce(lambda x, y: x | y, list4)
print(result1)
print(result2)
print(result3)
print(result4)

运行结果:

示例代码4:

from functools import reduce
def add(x, y):
    return x + y
a = [1, 2, 3, 4, 5]
#  reduce()两个参数
ret1 = reduce(add, a)
print(ret1)
#  reduce()三个参数
ret2 = reduce(add, a, 6)
print(ret2)

运行结果:

示例代码5: 【mongo和es语句中使用reduce】

from functools import reduce
from mongoengine import Q
from mongoengine.queryset.visitor import Q
from elasticsearch_dsl import Q as EQ
# query_list = ['x', 'y', 'z']  # 字符串报错:TypeError: unsupported operand type(s) for &: 'str' and 'str'
# query_list = [2, 3]  # 2
# query_list = [2, 3, 4]  # 0
# mongo中使用
query_list = [Q(aa="aa"), Q(bb='bb'), Q(cc='cc'), Q(dd='dd')]  # (Q(**{'aa': 'aa'}) & Q(**{'bb': 'bb'}) & Q(**{'cc': 'cc'}) & Q(**{'dd': 'dd'}))
res = reduce(lambda x, y: x & y, query_list)
print(res)
# es中使用
query_list = [EQ(aa="aa"), EQ(bb='bb'), EQ(cc='cc'), EQ(dd='dd')]  # MatchAll(dd='dd')
res = reduce(lambda x, y: x & y, query_list)
print(res)

运行结果:

到此这篇关于Python中reduce()函数的用法详细解读的文章就介绍到这了,更多相关Python中reduce()函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python 第一步 hello world

    Python 第一步 hello world

    Python 第一步 hello world 入门学习。
    2009-09-09
  • 实例讲解Python3中abs()函数

    实例讲解Python3中abs()函数

    在本篇文章里小编给大家分享了关于Python3中abs()函数的相关知识点内容,需要的朋友们跟着学习下。
    2019-02-02
  • 总结Python连接CS2000的详细步骤

    总结Python连接CS2000的详细步骤

    今天给大家带来的是关于Python的相关知识,文章围绕着Python连接CS2000的详细步骤展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • 使用Python绘制图表大全总结

    使用Python绘制图表大全总结

    本篇文章主要介绍了使用Python绘制图表大全总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 详解解Django 多对多表关系的三种创建方式

    详解解Django 多对多表关系的三种创建方式

    本文主要介绍了详解解Django 多对多表关系的三种创建方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • Python基础-特殊方法整理详解

    Python基础-特殊方法整理详解

    python中特殊方法(魔术方法)是被python解释器调用的,我们自己不需要调用它们,我们统一使用内置函数来使用。本篇文章将对其详细介绍,感兴趣的小伙伴可以参考下面文章的具体内容
    2021-09-09
  • python GoogleIt库实现在Google搜索引擎上快速搜索

    python GoogleIt库实现在Google搜索引擎上快速搜索

    这篇文章主要为大家介绍了python GoogleIt库实现在Google搜索引擎上快速搜索功能探究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • 详解HttpRunner3的HTTP请是如何发出

    详解HttpRunner3的HTTP请是如何发出

    这篇文章主要为大家介绍了HttpRunner3的HTTP请是如何发出详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Sublime Text4 配置 Python3 环境、代码提示、编译报错的解决方案

    Sublime Text4 配置 Python3 环境、代码提示、编译报错的解决方案

    这篇文章主要介绍了Sublime Text4 配置 Python3 环境、代码提示、编译报错教程,通过图文并茂的形式给大家介绍了配置自动代码提示的方法,需要的朋友可以参考下
    2022-01-01
  • 理解Python垃圾回收机制

    理解Python垃圾回收机制

    这篇文章主要为大家详细介绍了Python垃圾回收机制,Python中的垃圾回收以引用计数为主,分代收集为辅,想要深入理解Python垃圾回收机制,请阅读下文
    2016-02-02

最新评论