-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
103 lines (75 loc) · 2.63 KB
/
lambda.py
File metadata and controls
103 lines (75 loc) · 2.63 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
99
100
101
102
103
# Lambda Functions
# A lambda is an anonymous function and an anonymous function is a function that is defined without a name.
# https://www.programiz.com/python-programming/anonymous-function
# https://www.w3schools.com/python/python_lambda.asp
import datetime
# -------------------------------------------------------------------
# this is...
def double(x):
return x * 2
# nearly the same as this...
double = lambda x: x * 2
# 10
print(double(5))
# -------------------------------------------------------------------
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x % 2 == 0), my_list))
# [4, 6, 8, 12]
print(new_list)
# -------------------------------------------------------------------
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2, my_list))
# [2, 10, 8, 12, 16, 22, 6, 24]
print(new_list)
# -------------------------------------------------------------------
people = [
{"name": "John", "id": 1},
{"name": "Bill", "id": 4},
{"name": "Sandra", "id": 2},
{"name": "Jennifer", "id": 3},
]
# {'name': 'Bill', 'id': 4}
# {'name': 'Sandra', 'id': 2}
for person in filter(lambda i: i["id"] % 2 == 0, people):
print(person)
# -------------------------------------------------------------------
items = [15, 6, 1, 8]
# 1
# 6
# 8
# 15
for i in sorted(items):
print(i)
# 15
# 8
# 6
# 1
for i in sorted(items, reverse=True):
print(i)
# items.sort(key=lambda s: s[::-1])
# print(items)
# -------------------------------------------------------------------
# sort() — A method that modifies the list in-place
# sorted() — A built-in function that builds a new sorted list from an iterable
#
# lambda r:r[0] is an anonymous function with a single argument, r which would
# in this case was a list, e.g.: [datetime.datetime(2016, 7, 10, 0, 58, 54), "2.59"]
#
# the lambda then returns the first element of the list, in this case the element
# that corresponds to the datetime object. This is then used as the key for the sort.
list = [
[datetime.datetime(2016, 7, 10, 0, 58, 54), "2.59"],
[datetime.datetime(2016, 7, 10, 0, 58, 14), "2.68"],
[datetime.datetime(2016, 7, 10, 0, 57, 54), "2.61"],
[datetime.datetime(2016, 7, 10, 0, 58, 34), "2.61"],
]
list.sort(key=lambda r: r[0])
print(list)
# [[datetime.datetime(2016, 7, 10, 0, 57, 54), '2.61'], [datetime.datetime(2016, 7, 10, 0, 58, 14), '2.68'], [datetime.datetime(2016, 7, 10, 0, 58, 34), '2.61'], [datetime.datetime(2016, 7, 10, 0, 58, 54), '2.59']]
# -------------------------------------------------------------------
items = [5, 10, 15]
items[1]
# 10
data = dict(enumerate(items))
print(data)
# {0: 5, 1: 10, 2: 15}