-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.py
153 lines (93 loc) · 2.99 KB
/
lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Write a Python program to create a lambda function that adds 25 to a given number passed in as an argument.
"""
sample input: 10
sample output: 35
"""
list1 = list(map(int,input().split()))
ans = list(map(lambda x: x + 25,list1))
print(ans)
"""
Output:
12 23 32 10 20
[37, 48, 57, 35, 45]
"""
"""
Write a Python program to triple all numbers of a given list of integers. Use Python map.
Input:
sample list: [1, 2, 3, 4, 5, 6, 7]
Triple of list numbers:
[3, 6, 9, 12, 15, 18, 21]
"""
list2 = list(map(int,input().split()))
tripled = list(map(lambda x : x *3, list2))
print(tripled)
"""
Output:
1 2 3 4 5 6
[3, 6, 9, 12, 15, 18]
"""
"""
Write a Python program to square the elements of a list using map() function.
Sample List: [4, 5, 2, 9]
Square the elements of the list:
[16, 25, 4, 81]
"""
list3 = list(map(int,input().split()))
sqr = list(map(lambda n : n ** 2,list3))
print(sqr)
"""
Output:
4 5 2 9
[16, 25, 4, 81]
"""
"""
Write a Python program to print even numbers from the list
"""
list4 = list(map(int, input().split()))
is_even = list(filter(lambda x : x % 2 == 0,list4))
print(is_even)
"""
Output:
Enter the elements: 1 2 3 4 5 6 7
[2, 4, 6]
Here is some basic understanding of the functions like map, filter, reduce, and lambda.
1. filter Function:
Use Case: Filtering Elements in an Iterable:
The filter function is used to filter elements from an iterable (e.g., a list) based on a specified condition.
==> It takes a function and an iterable as arguments and returns a new iterable containing only the elements for which the function returns True.
Example:
"""
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_numbers = list(filter(lambda x: x % 2 == 0, numbers))
# Result: [2, 4, 6, 8, 10]
"""
2. map Function:
Use Case: Applying a Function to Each Element in an Iterable:
The map function applies a specified function to each element in an iterable and returns a new one with the results.
==> It takes a function and one or more iterables as arguments.
Example:
"""
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
# Result: [1, 4, 9, 16, 25]
"""
3. reduce Function:
Use Case: Aggregating Elements in an Iterable:
The reduce function is used to successfully apply a specified function to the elements of an iterable to reduce it to a single accumulated result.
==> It requires the functools module in Python 3 and takes a function and an iterable as arguments.
Example:
"""
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
# Result: 120
"""
4. Lambda Functions:
Use Case: Anonymous Functions:
Lambda functions are small, anonymous functions defined using the lambda keyword.
==> They are often used for short, one-time operations, especially as arguments to higher-order functions like filter, map, and reduce.
Example:
"""
add = lambda x, y: x + y
result = add(3, 5)
# Result: 8