Special grammars in Python: introduction to filter map reduce lambda

  • 2020-05-09 18:48:45
  • OfStack

filter(function, sequence) : execute function(item) for item in sequence, and form List/String/Tuple (depending on the type of sequence) into one List/String/Tuple (depending on the type of sequence) :


>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]
>>> def f(x): return x != 'a'
>>> filter(f, "abcdef")
'bcdef'

map(function, sequence) : execute function(item) for item in sequence, see the execution results to form a single List return:


>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def cube(x) : return x + x
...
>>> map(cube , "abcde")
['aa', 'bb', 'cc', 'dd', 'ee']

In addition, map also supports multiple sequence, which requires function to support a corresponding number of parameter input:

>>> def add(x, y): return x+y
>>> map(add, range(8), range(8))
[0, 2, 4, 6, 8, 10, 12, 14]

reduce(function, sequence, starting_value) : call function for item sequential iteration in sequence. If you have starting_value, you can also call function as an initial value, for example, you can sum over List:


>>> def add(x,y): return x + y
>>> reduce(add, range(1, 11))
55 (note: 1+2+3+4+5+6+7+8+9+10 )
>>> reduce(add, range(1, 11), 20)
75 (note: 1+2+3+4+5+6+7+8+9+10+20 )

lambda: this is an interesting syntax that Python supports. It allows you to quickly define single-line minimum functions, similar to macros in the C language. These functions, called lambda, are borrowed from LISP and can be used wherever you need them:


>>> g = lambda x: x * 2
>>> g(3)
6
>>> (lambda x: x * 2)(3)
6

We can also combine filter map reduce and lambda, and the function can simply be written as 1 line.
Such as:


kmpathes = filter(lambda kmpath: kmpath,                 
map(lambda kmpath: string.strip(kmpath),
string.split(l, ':')))    

It looks like a lot of trouble, but it's kind of like putting problem 1 into words, which is very elegant.
Split all elements in l with ':' to get a list. Make a string strip for each element in this list to form a list. Do a direct return operation on each element of the list (this can be filtered) to get a list of strings split by ':', each string in the list is strip, and special strings can be filtered.

---------------------------------------------------------------

The lambda expression returns a function object
Example:


func = lambda x,y:x+y
func It's the same thing as this function down here
def func(x,y):
    return x+y

Notice that def is a statement and lambda is an expression
In this case, you can only use lambda instead of def

[(lambda x:x*x)(x) for x in range(1,11)]

map, reduce, filter function can all be generated with lambda expressions!
 
map(function,sequence)
Pass the value arguments from sequence to function one by one, and return an list containing the result of the function's execution.
If there are two parameters function, namely map (function, sequence1, sequence2).
 
Example:
O 1 * 1, 2 * 2, 3 * 3, 4 * 4


map(lambda x:x*x,range(1,5))

The return value is [1,4,9,16]
 
reduce(function,sequence)

The number of parameters received by function can only be 2
First, pass the first value and second value of sequence to function, then pass the return value of function and the third value to function
function, and then only return 1 result.
 
Example:
So let's add 1 to 10


reduce(lambda x,y:x+y,range(1,11))

The return value is 55.
 
filter(function,sequence)

The return value of function can only be True or False
If the return value of function(x) is True, add x to the return value of filter. 1 generally speaking, the return value of filter is list. In special cases, if sequence is string or tuple, the return value is according to the type of sequence.
 
Example:
Find the odd number between 1 and 10


filter(lambda x:x%2!=0,range(1,11))

The return value

[1,3,5,7,9]

 
If sequence is one string

filter(lambda x:len(x)!=0,'hello') return 'hello'
filter(lambda x:len(x)==0,'hello') return ''


Related articles: