Skip to content

Commit 51a12a8

Browse files
committed
added some more functions(map,filter,reduce)
1 parent 59757c0 commit 51a12a8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

functions.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,58 @@ def display(self):
8989
stu.setAge(10)
9090
stu.setMarks(90)
9191
stu.display()
92+
93+
#filter function
94+
def fun1(a):
95+
return a%2==0
96+
l=[2,5,6,23,34,68,12]
97+
f1=filter(fun1,l)
98+
print(f1,type(f1))
99+
print(list(f1))#we can make use of filter only once
100+
l1=list(f1)
101+
print(l1)#we will get null
102+
103+
#lambda function
104+
v4=lambda x:x%2==0
105+
f2=filter(f4,l)
106+
for i in f2:
107+
print(i)
108+
109+
def fun1(a):
110+
return a**3
111+
print()
112+
print(fun1(3))
113+
print(fun1(5))
114+
v2=lambda x,y:x+y
115+
print()
116+
print(v2(2,7))
117+
118+
v3=lambda x,y:x if x>y else y
119+
print()
120+
print(v3(4,8))
121+
print(v3(6,3))
122+
123+
#map function
124+
l=[1,2,3,4,5,6,7]
125+
def fun2(a):
126+
return a*a
127+
128+
m1=map(fun2,l)
129+
print(list(m1),type(m1))
130+
131+
v=lambda x:x*x
132+
m2=map(v5,l)
133+
print(m2,list(m2),type(m1))
134+
#reduce function
135+
#reduces the sequence of elements into a single value
136+
#reduce will be imported from functools
137+
from functools import *
138+
l=[1,2,3,4,5,6,7]
139+
def fun3(a,b):
140+
return a+b
141+
r3= reduce(fun3,l)
142+
print(r3)
143+
144+
v6=lambda x,y:x+y
145+
r4=reduce(v6,l)
146+
print(r4)

0 commit comments

Comments
 (0)