官網(wǎng)解釋?zhuān)?/p>
創(chuàng)新互聯(lián)-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比永修網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式永修網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋永修地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴(lài)。
filter
(function, iterable)
Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None
, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable)
is equivalent to [item for item in iterable if function(item)]
if function is not None
and [item for itemin iterable if item]
if function is None
.
See itertools.ifilter()
and itertools.ifilterfalse()
for iterator versions of this function, including a variation that filters for elements where the function returns false.
str.
strip
([chars])
Return a copy of the string with the leading and trailing characters removed.
The chars argument is a string specifying the set of characters to be removed.
If omitted or None
, the chars argument defaults to removing whitespace.
The chars argument is not a prefix or suffix; rather, all combinations of its
values are stripped:
>>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example'
就是,如果省略參數(shù),默認(rèn)過(guò)濾空格。
#過(guò)濾list中的偶數(shù)
def get_odd(s):
return s % 2 == 1; #C語(yǔ)言絕對(duì)不能這么寫(xiě)
print "get odd:", filter(get_odd, [1, 2, 3, 4, 5, 6])
#過(guò)濾list中的空格
def not_empty(s):
return s and s.strip() #s過(guò)濾了'None'元素,s.strip()默認(rèn)過(guò)濾空格
print "not_empty:", filter(not_empty, ['A', ' ', 'B', None, 'C', ' '])
補(bǔ)充:
.strip(None/string):從頭尾開(kāi)始,按對(duì)象字符串順序刪除
a.如果strip參數(shù)中沒(méi)有對(duì)象頭尾開(kāi)始的字符,則無(wú)法刪除,返回對(duì)象全字符
str1 = 'www.example.com' print (str1.strip('lcoz.')) ####strip參數(shù)中沒(méi)有'w'/'m',兩端刪除受阻
b.如果strip參數(shù)中有對(duì)象的頭尾開(kāi)始字符,則匹配刪除參數(shù)中的字符,返回未刪除字符
str1 = 'www.example.com' print (str1.strip('lcwmeoza.')) xamp ####strip參數(shù)中沒(méi)有'x'/'p',兩端刪除受阻
網(wǎng)站標(biāo)題:python高階函數(shù):filter(過(guò)濾器)
標(biāo)題來(lái)源:http://sd-ha.com/article24/popgce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、自適應(yīng)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)