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)文章
詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式
本文主要介紹了詳解解Django 多對多表關(guān)系的三種創(chuàng)建方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-08-08python GoogleIt庫實現(xiàn)在Google搜索引擎上快速搜索
這篇文章主要為大家介紹了python GoogleIt庫實現(xiàn)在Google搜索引擎上快速搜索功能探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯的解決方案
這篇文章主要介紹了Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯教程,通過圖文并茂的形式給大家介紹了配置自動代碼提示的方法,需要的朋友可以參考下2022-01-01