Repository navigation
Expand file tree
/
Copy pathlambda.py
More file actions
184 lines (153 loc) · 4.39 KB
/
Copy pathlambda.py
File metadata and controls
184 lines (153 loc) · 4.39 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# lambda function : one liner function
"""def sum():
a= 10
b= 20
c =a+b
print(c)
sum()
"""
"""
syntax :
lambda arg : expression
"""
"""x =lambda a,b : a+b
print(x(10,20))
"""
# pow :
"""y =lambda x : x**2
a = int(input())
print(y(a))
"""
# len min max sorted :
"""a =lambda x : sum(x)
print(a([1,2,3,4,5,60,7,8,9,10]))
"""
# if else :
"""def big():
a = int(input("enter the a : "))
b = int(input("enter the b : "))
if a>b :
print("a is big")
else :
print("b is big")
big()
"""
"""x = lambda a,b : ("a is big ",a) if a>b else ("b is big",b)
print(x(100,200))
"""
# filter map :
"""
jan - dec ==> fin tran ==> june entry
"""
"""
l1 =[1,2,3,4,5,6,7,9,10]
a=tuple(filter(lambda x :x % 2==0,l1))
b=tuple(filter(lambda x :x % 2==1,l1))
print(sum(a))
print(sum(b))
"""
# map :
'''l1 =[1,2,3,4,5,6,7,9,10]
a=tuple(map(lambda x :x *5,l1))
print(a)
'''
#task:
'''Write a Python program to create a function that takes one argument, and that argument will be multiplied with an unknown given number.
Sample Output:
Double the number of 15 = 30
Triple the number of 15 = 45
Quadruple the number of 15 = 60
Quintuple the number 15 = 75
'''
'''a=lambda x : x*2
b=lambda x : x*3
c=lambda x : x*4
d=lambda x : x*5
num=int(input("Enter a number:"))
print(a(num))
print(b(num))
print(c(num))
print(d(num))
'''
#task:
'''Write a Python program to sort a list of tuples using Lambda.
Original list of tuples:
[('English', 88), ('Science', 90), ('Maths', 97), ('Social sciences', 82)]
Sorting the List of Tuples:
[('Social sciences', 82), ('English', 88), ('Science', 90), ('Maths', 97)]
'''
'''l1=[('English', 88), ('Science', 90), ('Maths', 97), ('Social sciences', 82)]
l1.sort(key=lambda x : x[1] )
print(l1)
'''
#task:
'''Write a Python program to sort a list of dictionaries using Lambda.
Original list of dictionaries :
[{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]
Sorting the List of dictionaries :
[{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}]
'''
'''l1=[{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]
l1.sort(key= lambda x : x['color'])
print(l1)
'''
#task:
'''Write a Python program to count the even and odd numbers in a given array of integers using Lambda.
Original arrays:
[1, 2, 3, 5, 7, 8, 9, 10]
Number of even numbers in the above array: 3
Number of odd numbers in the above array: 5
'''
'''l1=[1, 2, 3, 5, 7, 8, 9, 10]
a=tuple(filter(lambda x : x%2==0,l1))
b=tuple(filter(lambda x : x%2==1,l1))
print(len(a))
print(len(b))
'''
#task:
'''Write a Python program to find palindromes in a given list of strings using Lambda.
Orginal list of strings:
['php', 'w3r', 'Python', 'abcd', 'Java', 'aaa']
List of palindromes:
['php', 'aaa']
'''
'''l1=['php', 'w3r', 'Python', 'abcd', 'Java', 'aaa']
l2=[]
a=list(filter(lambda x : x == x[::-1], l1))
print(a)
'''
#task:
"""Write a Python program to remove specific words from a given list using lambda.
Original list:
['orange', 'red', 'green', 'blue', 'white', 'black']
Remove words:
['orange', 'black']
After removing the specified words from the said list:
['red', 'green', 'blue', 'white']"""
'''l1=['orange', 'red', 'green', 'blue', 'white', 'black']
l2=['orange','black']
a=list(filter(lambda x : x not in l2 ,l1))
print(a)
'''
#task:
"""Write a Python program to sort a given mixed list of integers and strings using lambda. Numbers must be sorted before strings.
Original list:
[19, 'red', 12, 'green', 'blue', 10, 'white', 'green', 1]
Sort the said mixed list of integers and strings:
[1, 10, 12, 19, 'blue', 'green', 'green', 'red', 'white']"""
'''l1=[19, 'red', 12, 'green', 'blue', 10, 'white', 'green', 1]
a=sorted(list(filter(lambda x : type(x) == int,l1)))
b=sorted(list(filter(lambda x : type(x) == str,l1)))
a.extend(b)
print(a)
'''
#task:
"""Write a Python program to remove None values from a given list using the lambda function.
Original list:
[12, 0, None, 23, None, -55, 234, 89, None, 0, 6, -12]
Remove None value from the said list:
[12, 0, 23, -55, 234, 89,0,6,-12]"""
'''1=[12, 0, None, 23, None, -55, 234, 89, None, 0, 6, -12]
a=list(filter(lambda x : x is not None ,l1))
print(a)
'''