Python reduce 函數 - Python零基礎入門教程


目錄

零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門

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

一.Python reduce 函數簡介

1.reduce 函數語法

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

"""

from functools import reduce # 導入模塊

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

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

2.reduce 函數原理

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

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

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

二.Python reduce 函數使用

1.reduce 函數普通使用

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

"""

from functools import reduce # 導入模塊

def func1(x,y):
    # 把上一次計算的結果作為下一次的計算的輸入
    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))

'''
輸出結果:

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'>
'''

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

2.reduce 函數配合匿名函數使用

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce 函數.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))

'''
輸出結果:

120
<class 'int'>
'''

3.reduce 函數設置可選參數 initial

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

"""


from functools import reduce # 導入模塊

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))


'''
輸出結果:

6000
<class 'int'>
'''

三.猜你喜歡

  1. Python 條件推導式
  2. Python 列表推導式
  3. Python 字典推導式
  4. Python 不定長參數 *argc/**kargcs
  5. Python 匿名函數 lambda
  6. Python return 邏輯判斷表達式
  7. Python is 和 == 區別
  8. Python 可變數據類型和不可變數據類型
  9. Python 淺拷貝和深拷貝
  10. Python 異常處理
  11. Python 線程創建和傳參
  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 線程優先隊列 PriorityQueue
  21. Python 線程池 ThreadPoolExecutor(一)
  22. Python 線程池 ThreadPoolExecutor(二)
  23. Python 進程 Process 模塊
  24. Python 進程 Process 與線程 threading 區別
  25. Python 進程間通信 Queue / Pipe
  26. Python 進程池 multiprocessing.Pool
  27. Python GIL 鎖

未經允許不得轉載:猿說編程 » Python reduce 函數

本文由博客 - 猿說編程 猿說編程 發布!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM