Python中filter與lambda的結(jié)合使用詳解
filter是Python的內(nèi)置方法。
官方定義是:
filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
第一個參數(shù)為None的情形:
filter(None, '101') # '101' filter(None, [True,False]) #[True] filter(None, [True, 0, 1, -1]) #[True, 1, -1] filter(None, (True, 1, 0, -1, False)) #(True, 1, -1)
第一個參數(shù)為function的情形,如果function(item)為True,則滿足過濾條件。此時的lambda函數(shù)的形式是: lambda x: expression(x)。
注意到,:左邊只能有一個元素x,:右邊為一個關(guān)于x的表達(dá)式,且這個表達(dá)式的值要么是True, 要么是False.
filter(lambda x: x, [-1, 0, 1]) #[-1, 1] filter(lambda x: not x, [-1, 0, 1]) #[0] def f(x): return True if x == 1 else False filter(lambda x: f(x), [-1, 0, 1]) #[1]
以上這篇Python中filter與lambda的結(jié)合使用詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 詳解python中的lambda與sorted函數(shù)
- Python使用lambda拋出異常實現(xiàn)方法解析
- Python lambda表達(dá)式原理及用法解析
- python zip,lambda,map函數(shù)代碼實例
- python匿名函數(shù)lambda原理及實例解析
- python lambda函數(shù)及三個常用的高階函數(shù)
- Python函數(shù)的返回值、匿名函數(shù)lambda、filter函數(shù)、map函數(shù)、reduce函數(shù)用法實例分析
- Python Lambda函數(shù)使用總結(jié)詳解
- Python三元運算與lambda表達(dá)式實例解析
- python lambda表達(dá)式(匿名函數(shù))寫法解析
- python lambda的使用詳解
相關(guān)文章
Python Django框架實現(xiàn)應(yīng)用添加logging日志操作示例
這篇文章主要介紹了Python Django框架實現(xiàn)應(yīng)用添加logging日志操作,結(jié)合實例形式分析了Django框架中添加Python內(nèi)建日志模塊相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Python+matplotlib實現(xiàn)簡單曲線的繪制
Matplotlib是Python的繪圖庫,它能讓使用者很輕松地將數(shù)據(jù)圖形化,并且提供多樣化的輸出格式。本文將利用matplotlib繪制簡單的曲線圖,感興趣的朋友可以學(xué)習(xí)一下2022-04-04
pyspark連接mysql數(shù)據(jù)庫報錯的解決
本文主要介紹了pyspark連接mysql數(shù)據(jù)庫報錯的解決,因為spark中缺少連接MySQL的驅(qū)動程序,下面就來介紹一下解決方法,感興趣的可以了解一下2023-11-11
python從內(nèi)存地址上加載python對象過程詳解
這篇文章主要介紹了python從內(nèi)存地址上加載pythn對象過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01

