目錄
零基礎 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'>
'''
三.猜你喜歡
- Python 條件推導式
- Python 列表推導式
- Python 字典推導式
- Python 不定長參數 *argc/**kargcs
- Python 匿名函數 lambda
- Python return 邏輯判斷表達式
- Python is 和 == 區別
- Python 可變數據類型和不可變數據類型
- Python 淺拷貝和深拷貝
- Python 異常處理
- Python 線程創建和傳參
- Python 線程互斥鎖 Lock
- Python 線程時間 Event
- Python 線程條件變量 Condition
- Python 線程定時器 Timer
- Python 線程信號量 Semaphore
- Python 線程障礙對象 Barrier
- Python 線程隊列 Queue – FIFO
- Python 線程隊列 LifoQueue – LIFO
- Python 線程優先隊列 PriorityQueue
- Python 線程池 ThreadPoolExecutor(一)
- Python 線程池 ThreadPoolExecutor(二)
- Python 進程 Process 模塊
- Python 進程 Process 與線程 threading 區別
- Python 進程間通信 Queue / Pipe
- Python 進程池 multiprocessing.Pool
- Python GIL 鎖
未經允許不得轉載:猿說編程 » Python reduce 函數
本文由博客 - 猿說編程 猿說編程 發布!