Python中reduce()函數(shù)的用法詳細(xì)解讀
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()有三個(gè)參數(shù),第一個(gè)是函數(shù)function,第二個(gè)是序列sequence,第三個(gè)是initial,為初始值,默認(rèn)為None
reduce(func,lst),其中func必須至少有兩個(gè)參數(shù)。每次func計(jì)算的結(jié)果繼續(xù)和序列的下?個(gè)元素做累積計(jì)算。
注意:reduce()傳?的參數(shù)func必須至少接收2個(gè)參數(shù)。
需求:計(jì)算 list1 序列中各個(gè)數(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)運(yùn)行結(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)運(yùn)行結(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)
運(yùn)行結(jié)果:

示例代碼4:
from functools import reduce
def add(x, y):
return x + y
a = [1, 2, 3, 4, 5]
# reduce()兩個(gè)參數(shù)
ret1 = reduce(add, a)
print(ret1)
# reduce()三個(gè)參數(shù)
ret2 = reduce(add, a, 6)
print(ret2)運(yùn)行結(jié)果:

示例代碼5: 【mongo和es語(yǔ)句中使用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'] # 字符串報(bào)錯(cuò):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)運(yùn)行結(jié)果:

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

