lambda expression in Python functional programming

Keywords: Python Lambda Programming

Definition of anonymous function
lambda parameter_list: expression

Two: ternary expression
Result returned when the condition is true if the condition determines the result returned when else condition is false

Three: map
map(func(arg1, arg2...), list1_arg1, list2_arg2),
Perform the previous functions (mathematical mapping) on the list entered later

Four: reduce
reduce(func(arg1, arg2...), list1_arg, init_value),
Continuous calculation, calling lambda expression continuously

V: filter
filter(func(arg1, arg2...), list1_arg1)
When the conditions are met, the data will be filtered out!

VI: functional programming and command programming
def
if --else
for

map reduce filter
lambda
The idea of functional programming....
The idea of imperative programming....
    Functional programming is concerned with data mapping, and imperative programming is concerned with problem solving steps

Functional programming:
(1) function is equal to other data types,
It can be assigned to other variables, passed in another function as parameters, or used as the return value of another function
(2) only use "expression" instead of "statement"

Code:

 1 from functools import reduce
 2 # ----------------------------------------------------------------#
 3 #   Definition of anonymous function
 4 # ----------------------------------------------------------------#
 5 
 6 
 7 def add(x, y):
 8     """
 9     add x and y
10     :param x: x can be str or num
11     :param y: y can be str or num
12     :return:  x+y
13     """
14     return x + y
15 
16 
17 # lambda parameter_list: expression
18 user_sum = lambda arg1, arg2: arg1 + arg2
19 
20 my_sum = user_sum(2, 2)
21 print(my_sum)
22 
23 # ----------------------------------------------------------------#
24 #   Ternary expression
25 # ----------------------------------------------------------------#
26 a, b = 1, 2
27 r = a if a > b else b
28 print(r)
29 
30 # ----------------------------------------------------------------#
31 #   map(func, list),For the following input list Perform the previous functions separately (mathematical mapping)
32 # ----------------------------------------------------------------#
33 
34 myListMap1 = [1, 2, 3, 4]
35 myNewListMap1 = map(lambda x: x ** 2, myListMap1)  # Return as map Data structure of type
36 print(type(myNewListMap1))
37 print('myNewListMap1:', list(myNewListMap1))    # Convert to list
38 
39 
40 # Of two or more parameters map Use of functions
41 # When the number of elements of two parameters is different, it will be truncated
42 myListMap2 = [1, 2, 3, 4]
43 myNewListMap2 = map(lambda x, y: x + y, myListMap1, myListMap2)
44 print('myNewListMap2:', list(myNewListMap2))
45 
46 
47 # ----------------------------------------------------------------#
48 #   reduce(func, list)Continuous computation lambda expression
49 # ----------------------------------------------------------------#
50 
51 myListReduce = [1, 2, 3, 4]
52 # hold list Value by value in lambda in
53 r = reduce(lambda x, y: x + y, myListReduce)
54 print(r)
55 
56 # Initialize the first function parameter
57 r = reduce(lambda x, y: x + y, myListReduce, 10)
58 print(r)
59 
60 # filter
61 myListFilter = [3, 5, 6, 7, 8, 9, 10]
62 myNewListFilter = filter(lambda x: x % 2 == 1, myListFilter)
63 print('myNewListFilter: ', list(myNewListFilter))
64 
65 list_x = [1, 1, 0, 0]
66 filter_list = filter(lambda x: True if x == 1 else False, list_x)
67 print(list(filter_list))

Posted by nando on Tue, 17 Dec 2019 13:36:42 -0800