欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中reduce()函數(shù)的用法詳細解讀

 更新時間:2023年08月21日 09:50:52   作者:IT之一小佬  
這篇文章主要介紹了Python中reduce()函數(shù)的用法詳細解讀,reduce函數(shù)是通過函數(shù)對迭代器對象中的元素進行遍歷操作,但需要注意的是?reduce?函數(shù)返回的是計算的結(jié)果,而?map/filter?返回的是作用后的迭代器對象,需要的朋友可以參考下

Python中的reduce()函數(shù)

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()有三個參數(shù),第一個是函數(shù)function,第二個是序列sequence,第三個是initial,為初始值,默認為None

reduce(func,lst),其中func必須至少有兩個參數(shù)。每次func計算的結(jié)果繼續(xù)和序列的下?個元素做累積計算。

注意:reduce()傳?的參數(shù)func必須至少接收2個參數(shù)。

需求:計算 list1 序列中各個數(shù)字的累加和。

示例代碼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)

運行結(jié)果:

示例代碼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)

運行結(jié)果:

示例代碼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)

運行結(jié)果:

示例代碼4:

from functools import reduce
def add(x, y):
    return x + y
a = [1, 2, 3, 4, 5]
#  reduce()兩個參數(shù)
ret1 = reduce(add, a)
print(ret1)
#  reduce()三個參數(shù)
ret2 = reduce(add, a, 6)
print(ret2)

運行結(jié)果:

示例代碼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)

運行結(jié)果:

到此這篇關(guān)于Python中reduce()函數(shù)的用法詳細解讀的文章就介紹到這了,更多相關(guān)Python中reduce()函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python 第一步 hello world

    Python 第一步 hello world

    Python 第一步 hello world 入門學習。
    2009-09-09
  • 實例講解Python3中abs()函數(shù)

    實例講解Python3中abs()函數(shù)

    在本篇文章里小編給大家分享了關(guān)于Python3中abs()函數(shù)的相關(guān)知識點內(nèi)容,需要的朋友們跟著學習下。
    2019-02-02
  • 總結(jié)Python連接CS2000的詳細步驟

    總結(jié)Python連接CS2000的詳細步驟

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Python連接CS2000的詳細步驟展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 使用Python繪制圖表大全總結(jié)

    使用Python繪制圖表大全總結(jié)

    本篇文章主要介紹了使用Python繪制圖表大全總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • 詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式

    詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式

    本文主要介紹了詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-08-08
  • Python基礎(chǔ)-特殊方法整理詳解

    Python基礎(chǔ)-特殊方法整理詳解

    python中特殊方法(魔術(shù)方法)是被python解釋器調(diào)用的,我們自己不需要調(diào)用它們,我們統(tǒng)一使用內(nèi)置函數(shù)來使用。本篇文章將對其詳細介紹,感興趣的小伙伴可以參考下面文章的具體內(nèi)容
    2021-09-09
  • python GoogleIt庫實現(xiàn)在Google搜索引擎上快速搜索

    python GoogleIt庫實現(xiàn)在Google搜索引擎上快速搜索

    這篇文章主要為大家介紹了python GoogleIt庫實現(xiàn)在Google搜索引擎上快速搜索功能探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 詳解HttpRunner3的HTTP請是如何發(fā)出

    詳解HttpRunner3的HTTP請是如何發(fā)出

    這篇文章主要為大家介紹了HttpRunner3的HTTP請是如何發(fā)出詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯的解決方案

    Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯的解決方案

    這篇文章主要介紹了Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯教程,通過圖文并茂的形式給大家介紹了配置自動代碼提示的方法,需要的朋友可以參考下
    2022-01-01
  • 理解Python垃圾回收機制

    理解Python垃圾回收機制

    這篇文章主要為大家詳細介紹了Python垃圾回收機制,Python中的垃圾回收以引用計數(shù)為主,分代收集為輔,想要深入理解Python垃圾回收機制,請閱讀下文
    2016-02-02

最新評論