lambda can be understood as a small anonymous functions, lambda functions can be used any number of parameters, but only one expression
template: lambda argument: manipulate (argument)
argument: argument is this anonymous function argument passed, after the colon is our method of operation of this parameter
Numbers = [1,2,3,4,5]
add_one = List (Map (the lambda n-: n-+. 1, Numbers))
Print (List (add_one))
[2,. 3,. 4, 5, 6]
map ()
main function map () function is a method can be performed sequentially in an iterative sequence, such as List, the specific information is as follows:
Basic grammar: map (fun, iterable)
parameters: fun map is passed to the function of each element may be given the iterative sequence. iterable can iterate a sequence, each sequence element can perform a fun
Return Value: map object
nums = [3,"23",-2]
print(list(map(float,nums)))
Out: [3.0, 23.0, -2.0]
filter()
the filter () function to filter by means of a given sequence, whether the function of each element of the test sequence is true.
Basic grammar: filter (fun, iterable)
parameters: fun Results of the test for each element of a sequence is iterable True, the filter may be iterable of iterative sequence
Return Value: iterative sequence may be, comprises elements for the implementation of the results are fun True to
first have a method to return True or False, or expression as a filter on the line
in a nutshell is filter can help us to filter data based on a set of conditions given and returns the result
DEF Fun (variable):
Letters = [ 'A', 'E', 'I', 'O', 'U']
IF (variable in Letters):
return True
the else:
return False
Sequence = [ 'the I', 'L', 'O', 'V', 'E', 'P', 'Y', 'T', 'H', 'O', 'n-']
Filtered = List (filter (Fun, Sequence))
Print (Filtered)
Sequence = [ 'I', 'l', 'o', 'v', 'e', 'p', 'y', 't', 'h', 'o', 'n']
result = filter(lambda x: x in ['a','e','i','o','u'],sequence)
print(list(result))
['o', 'e', 'o']
reduce()
Reduce is a useful function, for performing certain calculations on the list and returns the result. It will scroll continuously calculated values to the list. For example, if you want to calculate the cumulative list of integer multiplication or summation, etc.
Basic grammar: the reduce (function, iterable)
parameters: fun is continuously applied to the method iterable each element, a new parameter is the result of an execution, iterable to be filtered may be iterative sequence
Return Value: The ultimate function returns the result
from Import the reduce functools
Numbers = [1,2,3,4]
result_multiply the reduce = (the lambda X, Y: X * Y, Numbers)
result_add the reduce = (the lambda X, Y: X + Y, Numbers)
Print (result_multiply)
Print ( result_add)
24-
10