-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmap_function.py
More file actions
42 lines (32 loc) · 1.3 KB
/
Copy pathmap_function.py
File metadata and controls
42 lines (32 loc) · 1.3 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
# Basically, think of lambda expressions as being 1 time use functions that you create on the fly and discard after use. You don't necessarily name them anything (you can if you want to re-use them but it's not common).
#
# You define a lambda expression with the keyword lambda. For example lambda x: x*2 will return the doubled value of any number you give it (signified by *2). Think of x as the argument being passed in and think of anything to the right of the : being the return value of the function.
#
# If you use the map function in conjunction with a lambda expression on elements of the list defined below, what would the output be?
# Note: Map goes with collection or iterable.
l1 = [2,3,4,5,6]
l1 = list(map(lambda x: x**2, l1))
print(l1)
names = ["sapna", "ranu", "sandhya", "swati", "trishla"]
print(names)
names1 = list(map(str.capitalize, names))
for n in names1:
pass
print(names1)
# print(n)
# print(list(names1))
# for n in map(str.capitalize, names):
# print(n)
# print(", ".join(list((map(str.capitalize, names)))))
########################
# creating a custom map
# def my_map(f, lst):
# for i in lst:
# yield f(i)
# generator comprehension
def my_map(f, lst):
return (f(i) for i in lst)
o = my_map(str.capitalize, names)
print(next(o))
print(next(o))
print(list(o))