內建函式 filter和map

2022-08-11 02:03:14 字數 3287 閱讀 8152

filter

filter()函式接收乙個函式 f 和乙個list,這個函式 f 的作用是對每個元素進行判斷,返回 true或 false,filter()根據判斷結果自動過濾掉不符合條件的元素,返回由符合條件元素組成的新list。

例如,要從乙個list [1, 4, 6, 7, 9, 12, 17]中刪除偶數,保留奇數,首先,要編寫乙個判斷奇數的函式:

def

is_odd(x):

return x % 2 == 1

然後,利用filter()過濾掉偶數:

>>>list(filter(is_odd, [1, 4, 6, 7, 9, 12, 17]))

結果:

[1, 7, 9, 17]

利用filter(),可以完成很多有用的功能,例如,刪除 none 或者空字串:

def

is_not_empty(s):

return s and len(s.strip()) >0

>>>list(filter(is_not_empty, ['

test

', none, '', '

str', '

', '

end']))

結果:

['test', 'str', 'end']

注意: s.strip(rm) 刪除 s 字串中開頭、結尾處的 rm 序列的字元。

當rm為空時,預設刪除空白符(包括'\n', '\r', '\t', ' '),如下:

>>> a = ' 123'

>>> a.strip()

'123'

>>> a = '\t\t123\r\n'

>>> a.strip()

'123'

練習:請利用filter()過濾出1~100中平方根是整數的數,即結果應該是:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

方法:

import

math

defis_sqr(x):

return math.sqrt(x) % 1 ==0

print(list(filter(is_sqr, range(1, 101))))

結果:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

map

python中的map函式應用於每乙個可迭代的項,返回的是乙個結果list。如果有其他的可迭代引數傳進來,map函式則會把每乙個引數都以相應的處理函式進行迭代處理。map()函式接收兩個引數,乙個是函式,乙個是序列,map將傳入的函式依次作用到序列的每個元素,並把結果作為新的list返回。

有乙個list, l = [1,2,3,4,5,6,7,8],我們要將f(x)=x^2作用於這個list上,那麼我們可以使用map函式處理。

>>> l = [1,2,3,4,] 

>>> def

pow2(x):

...

return x*x

...

>>>list(map(pow2,l))

[1, 4, 9, 16]

filter

filter()函式接收乙個函式 f 和乙個list,這個函式 f 的作用是對每個元素進行判斷,返回 true或 false,filter()根據判斷結果自動過濾掉不符合條件的元素,返回由符合條件元素組成的新list。

例如,要從乙個list [1, 4, 6, 7, 9, 12, 17]中刪除偶數,保留奇數,首先,要編寫乙個判斷奇數的函式:

def

is_odd(x):

return x % 2 == 1

然後,利用filter()過濾掉偶數:

>>>list(filter(is_odd, [1, 4, 6, 7, 9, 12, 17]))

結果:

[1, 7, 9, 17]

利用filter(),可以完成很多有用的功能,例如,刪除 none 或者空字串:

def

is_not_empty(s):

return s and len(s.strip()) >0

>>>list(filter(is_not_empty, ['

test

', none, '', '

str', '

', '

end']))

結果:

['test', 'str', 'end']

注意: s.strip(rm) 刪除 s 字串中開頭、結尾處的 rm 序列的字元。

當rm為空時,預設刪除空白符(包括'\n', '\r', '\t', ' '),如下:

>>> a = ' 123'

>>> a.strip()

'123'

>>> a = '\t\t123\r\n'

>>> a.strip()

'123'

練習:請利用filter()過濾出1~100中平方根是整數的數,即結果應該是:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

方法:

import

math

defis_sqr(x):

return math.sqrt(x) % 1 ==0

print(list(filter(is_sqr, range(1, 101))))

結果:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

map

python中的map函式應用於每乙個可迭代的項,返回的是乙個結果list。如果有其他的可迭代引數傳進來,map函式則會把每乙個引數都以相應的處理函式進行迭代處理。map()函式接收兩個引數,乙個是函式,乙個是序列,map將傳入的函式依次作用到序列的每個元素,並把結果作為新的list返回。

有乙個list, l = [1,2,3,4,5,6,7,8],我們要將f(x)=x^2作用於這個list上,那麼我們可以使用map函式處理。

>>> l = [1,2,3,4,] 

>>> def

pow2(x):

...

return x*x

...

>>>list(map(pow2,l))

[1, 4, 9, 16]

內建函式 filter和map

filter filter 函式接收乙個函式 f 和乙個list,這個函式 f 的作用是對每個元素進行判斷,返回 true或 false,filter 根據判斷結果自動過濾掉不符合條件的元素,返回由符合條件元素組成的新list。例如,要從乙個list 1,4,6,7,9,12,17 中刪除偶數,保留...

內建函式filter

coding utf8 內建函式filter,filter和map 類似,filter 也接收乙個函式和乙個序列。和map 不同的時,filter 把傳入的函式依次作用於每個元素,然後根據返回值是true還是false決定保留還是丟棄該元素 defis odd x 判斷是否為奇數 return x ...

內建函式filter

filter 函式用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。該函式接收兩個引數,第乙個為函式,第二個為序列,序列的每個元素作為引數傳遞給函式進行判,然後返回 true 或 false,最後將返回 true 的元素放到新列表中。例 list0 1,2,3,4,5,6,7,8...