Filter built-in functions and built-in functions --filter map and map

Built-in functions and map --filter

 

filter

filter () function accepts a function f and a list, the role of this function f is determined for each element, returns True or False, filter () automatically filters out elements meet the conditions based on the result returned by the qualified element the new composition of the list.

For example, from a list [1, 4, 6, 7, 9, 12, 17] to remove an even number, odd retention, first, a determination function to write odd:

def is_odd(x):
    return x % 2 == 1

Then, the filter () to filter out the even:

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

result:

[1, 7, 9, 17]

Use filter (), you can perform many useful functions such as deleting None or the empty string:

def is_not_empty(s):
    return s and len(s.strip()) > 0
>>>list(filter(is_not_empty, ['test', None, '', 'str', '  ', 'END']))

result:

['test', 'str', 'END']

Note: s.strip (rm) s to delete the beginning of the string, the character sequence rm at the end.

When rm is empty, the default remove white space (including '\ n', '\ r', '\ t', ''), as follows:

>>> a = ' 123'
>>> a.strip()
'123'

>>> a = '\t\t123\r\n'
>>> a.strip()
'123'

Exercise:

Please use filter () 1-100 filtered square root is an integer number, i.e., the result should be:

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

method:

import math
def is_sqr(x):
    return math.sqrt(x) % 1 == 0
print(list(filter(is_sqr, range(1, 101))))

result:

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

map

Python is a map function to each item can be iterative, it returns a result list. If there are other parameters can be passed in the iteration, map function to each parameter will iterate a corresponding handler. map () function accepts two arguments, a function, a sequence is, map the incoming function sequentially applied to each element of the sequence, and to return the result as a new list.

There is a list, L = [1,2,3,4,5,6,7,8], we want to f (x) = x ^ 2 acting on the list, then we can use the map function processing.

>>> L = [1,2,3,4,] 
>>> def pow2(x): 
... return x*x 
... 
>>> list(map(pow2,L))
[1, 4, 9, 16] 

 

 

filter

filter () function accepts a function f and a list, the role of this function f is determined for each element, returns True or False, filter () automatically filters out elements meet the conditions based on the result returned by the qualified element the new composition of the list.

For example, from a list [1, 4, 6, 7, 9, 12, 17] to remove an even number, odd retention, first, a determination function to write odd:

def is_odd(x):
    return x % 2 == 1

Then, the filter () to filter out the even:

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

result:

[1, 7, 9, 17]

Use filter (), you can perform many useful functions such as deleting None or the empty string:

def is_not_empty(s):
    return s and len(s.strip()) > 0
>>>list(filter(is_not_empty, ['test', None, '', 'str', '  ', 'END']))

result:

['test', 'str', 'END']

Note: s.strip (rm) s to delete the beginning of the string, the character sequence rm at the end.

When rm is empty, the default remove white space (including '\ n', '\ r', '\ t', ''), as follows:

>>> a = ' 123'
>>> a.strip()
'123'

>>> a = '\t\t123\r\n'
>>> a.strip()
'123'

Exercise:

Please use filter () 1-100 filtered square root is an integer number, i.e., the result should be:

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

method:

import math
def is_sqr(x):
    return math.sqrt(x) % 1 == 0
print(list(filter(is_sqr, range(1, 101))))

result:

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

map

Python is a map function to each item can be iterative, it returns a result list. If there are other parameters can be passed in the iteration, map function to each parameter will iterate a corresponding handler. map () function accepts two arguments, a function, a sequence is, map the incoming function sequentially applied to each element of the sequence, and to return the result as a new list.

There is a list, L = [1,2,3,4,5,6,7,8], we want to f (x) = x ^ 2 acting on the list, then we can use the map function processing.

>>> L = [1,2,3,4,] 
>>> def pow2(x): 
... return x*x 
... 
>>> list(map(pow2,L))
[1, 4, 9, 16] 

 

Guess you like

Origin www.cnblogs.com/shangping/p/11332701.html