-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfr2.py
More file actions
55 lines (44 loc) · 1.06 KB
/
mfr2.py
File metadata and controls
55 lines (44 loc) · 1.06 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
from functools import reduce
# iterable
patients_dob = ['23-03-1981', '12-12-2005', '03-06-1950']
curr_yr = 2022
# function that works on a single item
def get_yob(dob):
return dob.split('-')[2]
# map function takes the above function and applies it to each item i the iterable
patients_yob = list(map(get_yob, patients_dob))
print(patients_yob)
def find_age(yob):
return curr_yr - int(yob)
ages = list(map(find_age, patients_yob))
print(ages)
def is_major(age):
return age >= 18
age_status = list(map(is_major, ages))
print(age_status)
adult_patients_ages = list(filter(is_major, ages))
print(adult_patients_ages)
# reduce example
age_list = [23, 34, 45, 56]
def add(a, b):
return a+b
age_sum = reduce(add, age_list)
print(age_sum)
def average_age1(ages):
return reduce(add, ages) / len(ages)
def average_age2(ages):
return reduce(lambda age1, age2: age1 + age2, ages) / len(ages)
print(average_age1(age_list))
print(average_age2(age_list))
Footer
© 2022 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Docs
Contact GitHub
Pricing
API
Train