Python reduce 函數(shù) - Python零基礎(chǔ)入門教程

目錄

零基礎(chǔ) Python 學(xué)習(xí)路線推薦 : Python 學(xué)習(xí)目錄 >> Python 基礎(chǔ)入門

Python 內(nèi)置函數(shù) reduce 和 map / filter 等函數(shù)有點類似,都是通過函數(shù)對迭代器中的元素進行遍歷操作,唯一區(qū)別是 reduce 函數(shù)是返回計算結(jié)果是一個值,而 map / filter 是返回一個序列或者迭代器,下面在做詳細(xì)解釋

一.Python reduce 函數(shù)簡介

1.reduce 函數(shù)語法

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce 函數(shù).py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

from functools import reduce # 導(dǎo)入模塊

'''
參數(shù)介紹:
    function – 有兩個參數(shù)的函數(shù), 必需參數(shù);
    sequence – tuple ,list ,dictionary, string等可迭代物,必需參數(shù);
    initial – 初始值, 可選參數(shù);

返回值:返回計算結(jié)果;
'''
reduce(function, sequence[, initial])

2.reduce 函數(shù)原理

reduce 函數(shù)的工作過程是 :在迭代 sequence(tuple ,list ,dictionary, string 等可迭代物)的過程中,首先把 前兩個元素傳給 函數(shù)參數(shù),函數(shù)加工后,然后把得到的結(jié)果和第三個元素作為兩個參數(shù)傳給函數(shù)參數(shù), 函數(shù)加工后得到的結(jié)果又和第四個元素作為兩個參數(shù)傳給函數(shù)參數(shù),依次類推。

如果傳入了 initial 值, 那么首先傳的就不是 sequence 的第一個和第二個元素,而是 initial 值和 第一個元素。經(jīng)過這樣的累計計算之后合并序列到一個單一返回值;

例如:reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 計算的就是((((1+2)+3)+4)+5) = 15

二.Python reduce 函數(shù)使用

1.reduce 函數(shù)普通使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce 函數(shù).py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

from functools import reduce # 導(dǎo)入模塊

def func1(x,y):
    # 把上一次計算的結(jié)果作為下一次的計算的輸入
    print("x=%d y=%d x*y=%d"%(x,y,x*y))
    return x*y

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = reduce(func1,list1) #等價 1*2*3*4*5 = 120
    print(value)
    print(type(value))

'''
輸出結(jié)果:

x=1 y=2 x*y=2
x=2 y=3 x*y=6
x=6 y=4 x*y=24
x=24 y=5 x*y=120
120
<class 'int'>
'''

實際上這個函數(shù)很簡單:把上一次計算的結(jié)果作為下一次的計算的輸入!

2.reduce 函數(shù)配合匿名函數(shù)使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce 函數(shù).py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = reduce(lambda x,y : x*y ,list1) #等價 1*2*3*4*5 = 120
    print(value)
    print(type(value))

'''
輸出結(jié)果:

120
<class 'int'>
'''

3.reduce 函數(shù)設(shè)置可選參數(shù) initial

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce 函數(shù).py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""


from functools import reduce # 導(dǎo)入模塊

def func1(x,y):
    return x*y

if __name__ == "__main__":
    list1 = [1,2,3,4,5]
    value = reduce(func1,list1,50) #等價 50*1*2*3*4*5 = 6000
    print(value)
    print(type(value))


'''
輸出結(jié)果:

6000
<class 'int'>
'''

三.猜你喜歡

  1. Python 條件推導(dǎo)式
  2. Python 列表推導(dǎo)式
  3. Python 字典推導(dǎo)式
  4. Python 不定長參數(shù) *argc/**kargcs
  5. Python 匿名函數(shù) lambda
  6. Python return 邏輯判斷表達(dá)式
  7. Python is 和 == 區(qū)別
  8. Python 可變數(shù)據(jù)類型和不可變數(shù)據(jù)類型
  9. Python 淺拷貝和深拷貝
  10. Python 異常處理
  11. Python 線程創(chuàng)建和傳參
  12. Python 線程互斥鎖 Lock
  13. Python 線程時間 Event
  14. Python 線程條件變量 Condition
  15. Python 線程定時器 Timer
  16. Python 線程信號量 Semaphore
  17. Python 線程障礙對象 Barrier
  18. Python 線程隊列 Queue – FIFO
  19. Python 線程隊列 LifoQueue – LIFO
  20. Python 線程優(yōu)先隊列 PriorityQueue
  21. Python 線程池 ThreadPoolExecutor(一)
  22. Python 線程池 ThreadPoolExecutor(二)
  23. Python 進程 Process 模塊
  24. Python 進程 Process 與線程 threading 區(qū)別
  25. Python 進程間通信 Queue / Pipe
  26. Python 進程池 multiprocessing.Pool
  27. Python GIL 鎖

未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python reduce 函數(shù)

本文由博客 - 猿說編程 猿說編程 發(fā)布!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容