欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 內(nèi)置函數(shù)filter

 更新時間:2017年06月01日 08:40:15   投稿:lqh  
這篇文章主要介紹了python 內(nèi)置函數(shù)filter的相關(guān)資料,需要的朋友可以參考下

python 內(nèi)置函數(shù)filter

class filter(object):
 """
 filter(function or None, iterable) --> filter object
 
 Return an iterator yielding those items of iterable for which function(item)
 is true. If function is None, return the items that are true.
 """

filter(func,iterator)

    func:自定義或匿名函數(shù)中所得值是布爾值,true將保留函數(shù)所取到的值,false則取反。
    iterator:可迭代對象。

例:

     過濾列表['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']
     只要含有text字符串及將其取出 or 取反。

s.rfind'text'+1

     Python3中 rfind() 返回字符串最后一次出現(xiàn)的位置,如果沒有匹配項(xiàng)則返回-1。
     數(shù)字中0是false,0以上的整數(shù)都是true,所以s.rfind'text'后會有+1,沒找到字符及-1+1=0.

# Filter

li = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']

# 默認(rèn)保留函數(shù)所取到的值
print(list(filter(lambda s: s.rfind('text') + 1, li)))
# 取反,下三個例子是一樣的
print(list(filter(lambda s: not s.rfind('text') + 1, li)))

# Noe 自定義函數(shù)

l1 = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']


def distinguish(l):
 nl = []
 for s in l:
  if s.rfind("text") + 1:
   nl.append(s)
 return nl


print(distinguish(l1))

# Two 自定義高階函數(shù)

l2 = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']


def f(s):
 return s.rfind('text') + 1


def distinguish(func, array):
 nl = []
 for s in array:
  if func(s):
   nl.append(s)
 return nl


print(distinguish(f, l2))

# Three 匿名函數(shù)

l3 = ['text_test_text', 'test_text_1', 'text_test_2', '3_test_text', 'test_test']


def distinguish(func, array):
 nl = []
 for s in array:
  if func(s):
   nl.append(s)
 return nl

print(distinguish(lambda s: s.rfind('text') + 1, l3))

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • python實(shí)現(xiàn)k均值算法示例(k均值聚類算法)

    python實(shí)現(xiàn)k均值算法示例(k均值聚類算法)

    這篇文章主要介紹了python實(shí)現(xiàn)k均值算法示例,簡單實(shí)現(xiàn)平面的點(diǎn)K均值分析,使用歐幾里得距離,并用pylab展示,需要的朋友可以參考下
    2014-03-03
  • python使用pipeline批量讀寫redis的方法

    python使用pipeline批量讀寫redis的方法

    今天小編就為大家分享一篇python使用pipeline批量讀寫redis的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • 用Cython加速Python到“起飛”(推薦)

    用Cython加速Python到“起飛”(推薦)

    這篇文章主要介紹了用Cython加速Python到“起飛”,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python通過http上傳文件思路詳解

    python通過http上傳文件思路詳解

    這篇文章主要介紹了python通過http上傳文件,在post請求中,用files參數(shù)來接受文件對象相關(guān)的參數(shù),通過data/json參數(shù)接受post請求體的其他參數(shù)
    2021-07-07
  • 使用selenium+chromedriver+xpath爬取動態(tài)加載信息

    使用selenium+chromedriver+xpath爬取動態(tài)加載信息

    這篇文章主要介紹了使用selenium+chromedriver+xpath爬取動態(tài)加載信息
    2022-02-02
  • python中reload重載實(shí)例用法

    python中reload重載實(shí)例用法

    在本篇文章里小編給大家整理的是一篇關(guān)于python中reload重載實(shí)例用法相關(guān)知識點(diǎn),有興趣的朋友們可以參考下。
    2020-12-12
  • python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例

    python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例

    今天小編就為大家分享一篇python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python集合基本概念與相關(guān)操作實(shí)例分析

    Python集合基本概念與相關(guān)操作實(shí)例分析

    這篇文章主要介紹了Python集合基本概念與相關(guān)操作,結(jié)合實(shí)例形式分析了Python集合的功能、原理、基本使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-10-10
  • python數(shù)據(jù)結(jié)構(gòu)之鏈表詳解

    python數(shù)據(jù)結(jié)構(gòu)之鏈表詳解

    這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)結(jié)構(gòu)之鏈表的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Python通過fnmatch模塊實(shí)現(xiàn)文件名匹配

    Python通過fnmatch模塊實(shí)現(xiàn)文件名匹配

    這篇文章主要介紹了Python通過fnmatch模塊實(shí)現(xiàn)文件名匹配,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評論