?? filter()函數(shù)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、宜章網(wǎng)站維護(hù)、網(wǎng)站推廣。
?????????? filter()函數(shù)是個(gè)過濾器,它的作用就是在海量的數(shù)據(jù)里提取出有用的信息
>>> help(filter)
Help on class filter in module builtins: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.
? |?
? |? Methods defined here:
? |?
? |? __getattribute__(self, name, /)
? |????? Return getattr(self, name).
? |?
? |? __iter__(self, /)
|????? Implement iter(self).
? |?
? |? __next__(self, /)
? |????? Implement next(self).
? |?
? |? __reduce__(...)
? |????? Return state information for pickling.
? |?
? |? ----------------------------------------------------------------------
? |? Static methods defined here:
? |?
? |? __new__(*args, **kwargs) from builtins.type
? |????? Create and return a new object.? See help(type) for accurate signature.?????? filter()函數(shù)有兩個(gè)參數(shù),第一個(gè)參數(shù)是一個(gè)函數(shù)也可以是None,第二個(gè)參數(shù)是可迭代對(duì)象iterable,如果第一個(gè)參數(shù)是
函數(shù),則將第二個(gè)參數(shù)可迭代對(duì)象的每個(gè)元素作為函數(shù)的參數(shù)進(jìn)行計(jì)算,把返回為True的值篩選出來;如果第一個(gè)參數(shù)是
None,則直接將第二個(gè)參數(shù)中為True的值篩選出來。
?????? 例:
>>> list1=filter(None,[1,0,2,True,False])
>>> list(list1)
[1, 2, True]>>> list(filter(lambda x:x%2,range(10)))
[1, 3, 5, 7, 9]