-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28-python-map-function.py
67 lines (42 loc) · 1.38 KB
/
28-python-map-function.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
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
numbers = range(0, 101)
def double(number):
return number * 2
doubled_iterator = map(double, numbers)
print(next(doubled_iterator))
print(list(doubled_iterator))
print(list(map(lambda x: x * 2, numbers)))
#######################################################################################################################
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
print(list(map(lambda x, y: x * y, a, b)))
#######################################################################################################################
c = [11, 12, 13, 14, 15]
print(list(map(lambda x, y, z: x * y * z, a, b, c)))
#######################################################################################################################
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "{} of {} years".format(self.name, self.age)
people = [
Person("John", 35),
Person("Marta", 25),
Person("Manuel", 12),
Person("Ruck", 15),
]
def increment_age(human):
human.age += 1
return human
people = map(increment_age, people)
for person in people:
print(person)
people = [
Person("John", 35),
Person("Marta", 25),
Person("Manuel", 12),
Person("Ruck", 15),
]
people = map(lambda human: Person(human.name, human.age + 1), people)
for person in people:
print(person)