forked from UCL-RITS/friend-group-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.py
54 lines (47 loc) · 1.22 KB
/
group.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
# Using the example code from the lecture
group = {
"Jill": {
"age": 26,
"job": "biologist",
"relations": {
"Zalika": "friend",
"John": "partner"
}
},
"Zalika": {
"age": 28,
"job": "artist",
"relations": {
"Jill": "friend"
}
},
"John": {
"age": 27,
"job": "writer",
"relations": {
"Jill": "partner"
}
},
"Nash": {
"age": 34,
"job": "chef",
"relations": {
"John": "cousin",
"Zalika": "landlord"
}
}
}
def mean(data):
"""Compute the mean of a non-empty list of numbers."""
return sum(data) / len(data)
print(max(person["age"] for person in group.values()))
print(mean([len(person["relations"]) for person in group.values()]))
print(max(person["age"] for person in group.values() if person["relations"]))
print(max(person["age"] for person in group.values() if "friend" in person["relations"].values()))
import json
#write file
with open('group_file.json', 'w') as json_file:
json.dumps(group, json_file, indent=4)
#read file
with open('group_file.json', 'r') as json_file:
group_data = json_file.read()