-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path13_Higherorder_functions.py
More file actions
73 lines (57 loc) · 1.47 KB
/
13_Higherorder_functions.py
File metadata and controls
73 lines (57 loc) · 1.47 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
# map()
s = [1,2,3,4]
print(list(map(int,s)))
# Converting map object to a list
a = [1,2,3,4,5,6]
def double(val):
return val**2
print(list(map(double,a)))
# map() with lambda
a = [8,3,4,2]
print(list(map(lambda x: x*3, a)))
# Using map() with multiple iterables
a = [1, 2, 3]
b = [4, 5, 6]
res = map(lambda x, y: x + y, a, b)
print(list(res))
# Converting to uppercase
fruits = ['apple', 'banana', 'cherry']
res = map(str.upper, fruits)
print(list(res))
# Extracting first character from strings
words = ['apple', 'banana', 'cherry']
res = map(lambda s: s[0], words)
print(list(res))
# Removing whitespaces from strings
s = [' hello ', ' world ', ' python ']
print(list(map(str.strip,s)))
# filter()
def even(n):
return n % 2 ==0
a = [1,2,3,4,5,6]
print(list(filter(even, a)))
# Using filter() with lambda
a = [1, 2, 3, 4, 5, 6]
print(list(filter(lambda x: x % 2 == 0, a)))
# Combining filter() with Other Functions
a = [1, 2, 3, 4, 5, 6]
b = filter(lambda x: x % 2 == 0, a)
c = map(lambda x: x * 2, b)
print(list(c))
# reduce()
from functools import reduce
def add(x,y):
return x + y
a = [1, 2, 3, 4]
print(reduce(add, a))
# Using reduce() with lambda
from functools import reduce
a = [1, 2, 3, 4]
print(reduce(lambda x, y: x + y, a))
# Using reduce() with operator functions
import functools
import operator
a = [1, 2, 3, 4]
print(functools.reduce(operator.add, a))
print(functools.reduce(operator.mul, a))
print(functools.reduce(operator.add, ["geeks", "for", "geeks"]))