-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_lambda.py
38 lines (30 loc) · 1.12 KB
/
map_lambda.py
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
# NOTE: map() does not modify the original list that was passed it.
# map() returns the modified list
# NOTE: map() returns a map object,
# so wrap list() around the map()
# to read the new list
# NOTE: abs( x) x can be a float too; not just integer
from functools import reduce
lst = [-56, -7.8]
print(f"\nOriginal list: {lst}")
print(list(map(lambda x: abs(x), lst)))
print("mapping lambda x: abs(x)")
print(f"Original list, after map: {lst}")
lst = ["56", "78"]
print(f"\nOriginal list: {lst}")
print(list(map(lambda x: int(x), lst)))
print("mapping lambda x: int(x)")
print(f"Original list, after map: {lst}")
lst = ['blue', "RED", 'Yellow']
print(f"\nOriginal list: {lst}")
print(list(map(lambda x: x.swapcase(), lst)))
print("mapping lambda x: x.swapcase()")
print(f"Original list, after map: {lst}")
# are all words in lowercase?
lst = ['blue', "RED", 'Yellow']
print(f"\nOriginal list: {lst}")
# first map all words to whether it's lowercase or not
print("mapping lambda x: x.islower()")
m = map(lambda x: x.islower(), lst)
# then check that every result is True
print(reduce(lambda a, b: bool(a and b), m))