目錄
- 一.map 函式
- 二.reduce 函式
- 三.filter 函式
- 四.猜你喜歡
零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門
Python 中 reduce / map / filter 三個函式很容易搞混淆,雖然利用函式對迭代器或者序列中的元素操作,但是適用的場景卻各不相同;
一.map 函式
map 函式特點:對可迭代器或者序列中的每個元素進行相同的操作(例如每個元素+1 等等),并回傳迭代器或者串列,示例如下:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函式區別.py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
def func1(x):
# 將每一個元素計算平方值
print("x=%d x*x=%d"%(x,x*x))
return x*x
if __name__ == "__main__":
list1 = [1,2,3,4,5]
#方法一:
value = https://www.cnblogs.com/shuopython/archive/2021/06/21/map(func1,list1) #回傳map物件,可以強制轉為list串列
print(list(value))
print("***"*20)
#方法二:
value = https://www.cnblogs.com/shuopython/archive/2021/06/21/map(lambda x:x*x, list1) #回傳map物件,可以強制轉為list串列
print(list(value))'''
輸出結果:
x=1 x*x=1
x=2 x*x=4
x=3 x*x=9
x=4 x*x=16
x=5 x*x=25
[1, 4, 9, 16, 25]
************************************************************
[1, 4, 9, 16, 25]
'''
值得注意的是:map 函式回傳值是迭代器,注意回傳的結果只能迭代一次,如果需要多次使用請提前保存結果并處理,例如:
def func1(x):
# 將每一個元素計算平方值
# print("x=%d x*x=%d"%(x,x*x))
return x*x
if __name__ == "__main__":
list1 = [1,2,3,4,5]
value = https://www.cnblogs.com/shuopython/archive/2021/06/21/map(func1,list1) #回傳map物件,可以強制轉為list串列
print(list(value))
print(list(value))'''
輸出:
[1, 4, 9, 16, 25]
[]
'''
很懵逼是不是?明明沒什么錯誤,為什么第二次輸出就是空串列呢?因為 map 函式回傳的迭代器只能迭代一次,解決辦法:在獲取結果的時候強轉為 list 串列 即可,實體如下:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函式區別.py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
def func1(x):
# 將每一個元素計算平方值
# print("x=%d x*x=%d"%(x,x*x))
return x*x
if __name__ == "__main__":
list1 = [1,2,3,4,5]
value = https://www.cnblogs.com/shuopython/archive/2021/06/21/list(map(func1,list1)) #回傳map物件,可以強制轉為list串列
print(list(value))
print(list(value))'''
輸出:
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
'''
二.reduce 函式
reduce 函式特點:從左到右對一個序列的項累計地應用有兩個引數的函式,以此合并序列到一個單一值(例如累加或累乘串列元素等等),回傳最終的計算結果,是一個值,示例如下:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函式區別.py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
#python3在使用reduce函式時需要匯入模塊
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 = https://www.cnblogs.com/shuopython/archive/2021/06/21/reduce(func1,list1) #等價 1*2*3*4*5 = 120
print(value)
print(type(value))
print("***"*20)
#方法二:
value = https://www.cnblogs.com/shuopython/archive/2021/06/21/reduce(lambda x,y:x*y, 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'>
************************************************************
120
<class 'int'>
'''
三.filter 函式
filter 函式特點:對可迭代物件中的元素按照特定的條件進行篩選(例如篩選串列中所有的偶數等等),示例如下:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python reduce / map / filter 函式區別.py
@Time:2021/05/18 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
lis=[0,1,2,3,4,5,6]
#定義篩選偶數的普通函式
def func4(x):
return x%2==0
#第一種使用filter函式的方式---lambda
res5=filter(lambda x:x%2==0,lis)
print(list(res5))
print(list(res5))
print("***"*20)
#第二種使用filter函式的方式---普通函式二
res7=filter(func4,lis)
print(list(res7))
print(list(res7))
'''
輸出結果:
[0, 2, 4, 6]
[]
************************************************************
[0, 2, 4, 6]
[]
'''
懵逼?事實證明,filter 函式回傳的結果也和 map 函式一樣,只能迭代一次,解決方案和 map 的解決方案一樣,在獲取結果的時候強轉為** list 串列** 即可;
四.猜你喜歡
- 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 / map / filter 函式區別
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288264.html
標籤:其他
