-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_sample_code.py
More file actions
58 lines (49 loc) · 1.67 KB
/
lambda_sample_code.py
File metadata and controls
58 lines (49 loc) · 1.67 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
###### Arithmetic ######
add = lambda x, y : x + y
print(f'Rslt = {add(5, -9)}')
###### List Comprehension ######
num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
squares = list(map(lambda x: x ** 2, num_list))
print(f'\n Squares: \n {squares}')
###### Lambda Key ######
dict_alpha = [(1, 'z'), (2, 'a'), (3, 'b')]
sorted_list = sorted(dict_alpha, key=lambda x: x[1])
print(f'\n Sorted List: \n {sorted_list}')
p_info = [
]
pii = [
(
)
]
# sort by name
contacts = sorted(p_info, key=lambda x: x[1]['name'])
print(f'\n p_info: \n {contacts}')
contacts = sorted(pii, key=lambda x: x[1:])
print(f'\n pii: \n {contacts}')
# sort by email
contacts = sorted(p_info, key=lambda x: x[1]['email'])
print(f'\n p_info: \n {contacts}')
contacts = sorted(pii, key=lambda x: x[2:])
print(f'\n PII: \n {contacts}')
####### Filter ######
num_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
hash_keys = list(filter(lambda x: x % 3 == 0, num_list))
print(f'\n Hash_keys for pos: \n {hash_keys}')
####### Dictionary ######
ops = {
'+': lambda x, y: x + y,
'-': lambda x, y: x-y
}
print(f'\n {ops["+"](2, -12)}')
print(f'\n {ops["-"](2, -12)}')
####### Reduce ######
from functools import reduce
num_list = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, num_list)
print(f'\n reduce: Product: {product}')