-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.py
More file actions
98 lines (73 loc) · 2.31 KB
/
map.py
File metadata and controls
98 lines (73 loc) · 2.31 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
# check if the given list of numbers are even and odd
l = [2, 3, 5, 6, 7, 8]
e_o = lambda num: "even number" if num % 2 == 0 else "odd number"
y = map(e_o, l)
print(list(y))
######################################################################
# convert -ve numbers to +ve numbers
l = [2, -3, -4, -5]
convert = lambda num: abs(num)
print(list(map(convert, l)))
print(list(map(abs, l)))
########################################################################
# find if a given string starts with a vowels
l = ['apple', 'orange', 'gmail', 'yahoo']
def vowels(string):
if string[0].lower() in 'aeiou':
return string
else:
return "not vowel"
print(list(map(vowels,l)))
#####################################################################
# square and cube every number in a given list of integer
l = [2, 3, 4, 5]
s_c = lambda i: [i**2, i**3]
print(list(map(s_c, l)))
#####################################################################
# return list of elements raised power of their indices
l = [2, 3, 4, 5]
l_ = []
for index, item in enumerate(l):
res = item**index
l_.append(res)
print(l_)
def power(num):
for index, item in enumerate(num):
res = item**index
print(res)
power(l)
l = [2, 3, 4, 5]
def power(num):
index, item = num
return item ** index
print(list(map(power,enumerate(l))))
"""
deep unpacking
((a, b),) = ((1, 2),)
print(a)
print(b)
"""
l = [2, 3, 4, 5]
def power_index(element):
index, item = element
return item ** index
print(list(map(power_index, enumerate(l))) ) # (0, 2)
########################################################################
# l1 = [1,2,3,4] l2 = [5,6,7,8] add the following list simultaneously
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
def sum_(x,y):
return x + y
print(list(map(sum_, l1, l2)))
##########################################################################
# s = "hi good afternoon" print the list of length of each word
s = "hi good afternoon"
l = s.split()
print(list(map(len, l)))
#####################################################################
#s = "hi good afternoon" returns all the words in upper case
s = "hi good afternoon"
l = s.split()
def upper_(n):
return n.upper()
print(list(map(str.upper,l)))