網站首頁 編程語言 正文
前言
Python3中的map()、reduce()、filter() 這3個一般是用于對序列進行操作的內置函數,它們經常需要與 匿名函數 lambda 聯合起來使用,我們今天就來學習下。
1.map()
map() 可以用于在函數中對指定序列做映射,返回值是一個迭代器,其使用語法如下:
map(function, *iterables)
上面的第一個參數 function 指一個函數,第二個參數 iterable 指一個或多個可迭代對象,在執行過程中,會對可迭代對象中的每一個元素調用 function 函數做計算,最后得到一個新的迭代器對象,而這個新的迭代器對象,會包含有每次調用 function 函數的返回值。
只傳入一個可迭代對象:
"""計算列表中每個元素的三次方"""
def demo_map(x):
return x ** 3
nums = [1, 2, 3, 4, 5]
print(list(map(demo_map, nums))) # 輸出:[1, 8, 27, 64, 125]
# 使用匿名函數
print(list(map(lambda x: x ** 3, nums))) # 輸出:[1, 8, 27, 64, 125]
傳入多個可迭代對象
"""計算3個列表中對應下標元素的和"""
def demo_map(x, y, z):
return x + y + z
nums1 = [1, 2, 3, 4, 5]
nums2 = [11, 22, 33, 44, 55]
nums3 = [100, 200, 300, 400, 500]
print(list(map(demo_map, nums1, nums2, nums3))) # 輸出:[112, 224, 336, 448, 560]
# 使用匿名函數
print(list(map(lambda x, y, z: x + y + z, nums1, nums2, nums3))) # 輸出:[112, 224, 336, 448, 560]
2.filter()
filter() 可以用于過濾序列,過濾掉不符合條件的元素,返回值也是一個迭代器,其使用語法如下:
filter(function or None, iterable)
和 map() 函數類似,上面的第一個參數 function 指一個函數,第二個參數 iterable 指一個可迭代對象,執行后會得到一個包含每次調用 function 函數返回值的迭代器。
"""找出從 -5 到 5 中能被 4 整除的所有整數"""
def demo_filter(x):
return x % 4 == 0
nums = range(-5, 6)
print(list(nums)) # 輸出:[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
# 傳入None,只返回true的值(0是False,所以被過濾掉了)
print(list(filter(None, nums))) # 輸入:[-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]
# 傳入正常函數,過濾出 nums 中能被 4 整除的整數
print(list(filter(demo_filter, nums))) # 輸出:[-4, 0, 4]
# 使用匿名函數
print(list(filter(lambda x: x % 4 == 0, nums))) # 輸出:[-4, 0, 4]
針對 map() 和 filter() 函數, 這里有 2 點需要注意:
- map 中必須傳入一個正常函數,而在 filter 函數中則可以傳正常函數或者None,當傳入None時,只返回可迭代對象中所有符合 true 的值
- map 中支持傳多個可迭代對象,而在 filter 函數中則只能傳一個可迭代對象
3.reduce()
reduce() 可以用于對參數序列中的元素進行累積,返回的是一個值。
在 Python3 中,reduce() 已被從全局名字空間里移除了,如果想要使用它,那么需通過引入 functools 模塊來調用 reduce() 函數,
其使用語法如下:
from functools import reduce
reduce(function, sequence[, initial])
上面的第一個參數 function 指一個函數,并且該函數必須含有2個參數,第二個參數 sequence 指一個序列,第三個參數 initial 指初始值,默認是None。
例如存在函數:reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]),它就相當于 ((((1+2)+3)+4)+5)。
from functools import reduce
def demo_reduce(x, y):
return x + y
nums = range(1, 101)
print(reduce(demo_reduce, nums)) # 輸出:5050
# 使用匿名函數
print(reduce(lambda x, y: x + y, nums)) # 輸出:5050
# 設置初始值為 1000
print(reduce(lambda x, y: x + y, nums, 1000)) # 輸出:6050
上面的 map()、reduce()、filter() 都是屬于Python3中的高階函數,它們最大的好處在于可以讓代碼更加簡潔,當然,如果不使用它們,我們也可以通過其他方式來實現。
原文鏈接:https://blog.csdn.net/qdPython/article/details/126143564
相關推薦
- 2023-10-13 ECharts日歷熱力圖點擊事件和選中日期加邊框
- 2024-07-15 Spring Boot多環境指定yml或者properties
- 2023-01-09 Android自定義ViewGroup實現九宮格布局_Android
- 2022-11-13 一文詳解Go語言單元測試的原理與使用_Golang
- 2023-04-13 next 配置全局scss變量、函數
- 2022-04-28 WPF使用WrapPanel環繞面板布局_實用技巧
- 2022-10-19 react實現動態選擇框_React
- 2022-04-19 python獲取http請求響應頭headers中的數據的示例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支