From 19c70b79243ad46e299657248314d384641a5fb7 Mon Sep 17 00:00:00 2001 From: Arianna Date: Mon, 26 Oct 2020 18:51:57 +0000 Subject: [PATCH 1/2] Added the requested changes to the file group.py --- group.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/group.py b/group.py index e2ec347..4f636a6 100644 --- a/group.py +++ b/group.py @@ -1,5 +1,72 @@ -"""An example of how to represent a group of acquaintances in Python.""" +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" + } + } +} + + +#the maximum age of people in the group +import numpy as np + +ages=[] + +for person in group: + ages.append(group[person]["age"]) +print('maximum age of people in the group is', np.max(ages)) + + + +#the average (mean) number of relations among members of the group +relation = [] +for person in group: + #print(group[person]["relations"]) + relation.append(len(group[person]["relations"].keys())) +print('average number of relations among members of the group is', np.mean(relation)) + + +#the maximum age of people in the group that have at least one relation +age_n = [] +for person in group: + if len(group[person]["relations"].keys()) >= 1: + age_n.append(group[person]["age"]) +print('maximum age of people in the group that have at least one relation is', np.max(age_n)) + + + +#[more advanced] the maximum age of people in the group that have at least one friend +Ages = [] +for person in group: + if "friend" in group[person]["relations"].values(): + Ages.append(group[person]["age"]) + +print('The maximum age of people in the group that have at least one friend is', np.max(Ages)) + -# Your code to go here... -my_group = From a984bf6277b84f1c99839af072bbdb10e84ec4e4 Mon Sep 17 00:00:00 2001 From: Arianna Date: Wed, 28 Oct 2020 15:34:24 +0000 Subject: [PATCH 2/2] Reading and writing structured data files exercise --- group.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/group.py b/group.py index 4f636a6..6d9fd63 100644 --- a/group.py +++ b/group.py @@ -70,3 +70,15 @@ + +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() + +