Python中reduce()函數(shù)的用法詳細解讀
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?實現(xiàn)?redis?數(shù)據(jù)庫的操作
這篇文章主要介紹了python?包?redis?數(shù)據(jù)庫的操作教程,redis?是一個?Key-Value?數(shù)據(jù)庫,下文基于python的相關(guān)資料展開對redis?數(shù)據(jù)庫操作的詳細介紹,需要的小伙伴可以參考一下2022-04-04
Python qqbot 實現(xiàn)qq機器人的示例代碼
這篇文章主要介紹了Python qqbot 實現(xiàn)qq機器人的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Python輕量級ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了Python輕量級ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法,結(jié)合實例形式較為詳細的分析了ORM框架的概念、功能及peewee的安裝、使用及操作sqlite數(shù)據(jù)庫的方法,需要的朋友可以參考下2017-07-07
解析numpy中的iscomplex方法及實際應(yīng)用
NumPy 的 iscomplex 方法為檢查數(shù)組中的元素是否為復數(shù)提供了一種高效且易于使用的接口,本文介紹了 iscomplex 方法的基本概念、使用方法以及它在解決實際問題中的應(yīng)用,需要的朋友可以參考下2024-06-06
使用python 寫一個靜態(tài)服務(wù)(實戰(zhàn))
今天小編就為大家分享一篇使用python 寫一個靜態(tài)服務(wù)(實戰(zhàn)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python實現(xiàn)在線暴力破解郵箱賬號密碼功能示例【測試可用】
這篇文章主要介紹了Python實現(xiàn)在線暴力破解郵箱賬號密碼功能,結(jié)合完整實例形式分析了Python讀取txt字典文件針對郵箱的相關(guān)驗證破解操作技巧,需要的朋友可以參考下2017-09-09

