From 1a57e41438e721c16f9a4f2b35f983a8de88b832 Mon Sep 17 00:00:00 2001 From: Time Spain Date: Tue, 27 Oct 2020 02:14:41 +0800 Subject: [PATCH 1/4] Exercise for week3 --- group.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..ced7ca6 100644 --- a/group.py +++ b/group.py @@ -1,5 +1,49 @@ """An example of how to represent a group of acquaintances in Python.""" # Your code to go here... +my_group = { + 'Jill' : { + 'age': 26, + 'job': 'biologist', + 'connection': { + 'Zalika':'friend', + 'John':'partner' + }}, + 'Zalika' : { + 'age': 28, + 'job': 'artist', + 'connection': { + 'Jill':'friend' + }}, + 'John' : { + 'age': 27, + 'job': 'writer', + 'connection': { + 'Jill':'partner' + }}, + 'Nash' : { + 'age': 34, + 'job': 'chef', + 'connection': { + 'John':'cousin', + 'Zalika':'landlord' + }} +} -my_group = +print(my_group.items()) + +#Question 1# +age_1 = {friend: person["age"] for friend, person in my_group.items()} +print("The maximum age in the group is:", max(age_1.values())) + +#Question 2# +no_relations = {friend: len(my_group[friend]['connection']) for friend, person in my_group.items()} +print("The average (mean) number of relations among members of the group: is", sum([x for x in no_relations.values()])/len(no_relations)) + +#Question 3# +age_2 = {friend: person["age"] for friend, person in my_group.items() if len(my_group[friend]['connection'])>=1} +print("The maximum age of people in the group that have at least one relation is:", max(age_2.values())) + +#Question 4# +age_3 = {friend: person["age"] for friend, person in my_group.items() if "Friend" in my_group[friend]['connection'][0].values()} +print("The maximum age of people in the group that have at least one friend is:", max(age_3.values())) \ No newline at end of file From a64dd2383c2762ed28eaa36b8e318c5a664e387c Mon Sep 17 00:00:00 2001 From: Time Spain Date: Wed, 28 Oct 2020 16:41:06 +0800 Subject: [PATCH 2/4] Answer to question #11 (JSON) --- group.py | 78 ++++++++++++++++++++++++++++++++------------------- my_group.json | 33 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 29 deletions(-) create mode 100644 my_group.json diff --git a/group.py b/group.py index ced7ca6..2770c4b 100644 --- a/group.py +++ b/group.py @@ -1,34 +1,42 @@ """An example of how to represent a group of acquaintances in Python.""" # Your code to go here... + +import json + my_group = { - 'Jill' : { - 'age': 26, - 'job': 'biologist', - 'connection': { - 'Zalika':'friend', - 'John':'partner' - }}, - 'Zalika' : { - 'age': 28, - 'job': 'artist', - 'connection': { - 'Jill':'friend' - }}, - 'John' : { - 'age': 27, - 'job': 'writer', - 'connection': { - 'Jill':'partner' - }}, - 'Nash' : { - 'age': 34, - 'job': 'chef', - 'connection': { - 'John':'cousin', - 'Zalika':'landlord' - }} -} + "Jill": { + "age": 26, + "job": "biologist", + "connection": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "connection": { + "Jill": "friend" + } + }, + "John": { + "age": 27, + "job": "writer", + "connection": { + "Jill": "partner" + } + }, + "Nash": { + "age": 34, + "job": "chef", + "connection": { + "John": "cousin", + "Zalika": "landlord" + } + } +} + print(my_group.items()) @@ -45,5 +53,17 @@ print("The maximum age of people in the group that have at least one relation is:", max(age_2.values())) #Question 4# -age_3 = {friend: person["age"] for friend, person in my_group.items() if "Friend" in my_group[friend]['connection'][0].values()} -print("The maximum age of people in the group that have at least one friend is:", max(age_3.values())) \ No newline at end of file +age_3 = {friend: person["age"] for friend, person in my_group.items() if "friend" in my_group[friend]['connection'].values()} +print("The maximum age of people in the group that have at least one friend is:", max(age_3.values())) + +# Change the group dictionary into the json file +with open('my_group.json', 'r') as json_file: + print(json.dump(my_group, json_file, indent=4)) + +#Loading data +with open('my_group.json', 'r') as json_file: + my_group_as_string = json_file.read() + print(my_group_as_string) + + mygroup = json.loads(my_data_as_string) + print(mygroup) \ No newline at end of file diff --git a/my_group.json b/my_group.json new file mode 100644 index 0000000..5578230 --- /dev/null +++ b/my_group.json @@ -0,0 +1,33 @@ +{ + "Jill": { + "age": 26, + "job": "biologist", + "connection": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "connection": { + "Jill": "friend" + } + }, + "John": { + "age": 27, + "job": "writer", + "connection": { + "Jill": "partner" + } + }, + "Nash": { + "age": 34, + "job": "chef", + "connection": { + "John": "cousin", + "Zalika": "landlord" + } + } +} + From d65d89eb863948cd62eb9d0c4a0980204dcda6e4 Mon Sep 17 00:00:00 2001 From: Time Spain Date: Wed, 28 Oct 2020 17:01:28 +0800 Subject: [PATCH 3/4] Answer to Question 11 (JSON) --- group.py | 2 +- my_group,json | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 my_group,json diff --git a/group.py b/group.py index 2770c4b..c44b9f9 100644 --- a/group.py +++ b/group.py @@ -60,7 +60,7 @@ with open('my_group.json', 'r') as json_file: print(json.dump(my_group, json_file, indent=4)) -#Loading data +#Loading Data with open('my_group.json', 'r') as json_file: my_group_as_string = json_file.read() print(my_group_as_string) diff --git a/my_group,json b/my_group,json new file mode 100644 index 0000000..5578230 --- /dev/null +++ b/my_group,json @@ -0,0 +1,33 @@ +{ + "Jill": { + "age": 26, + "job": "biologist", + "connection": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "connection": { + "Jill": "friend" + } + }, + "John": { + "age": 27, + "job": "writer", + "connection": { + "Jill": "partner" + } + }, + "Nash": { + "age": 34, + "job": "chef", + "connection": { + "John": "cousin", + "Zalika": "landlord" + } + } +} + From 00fc3972d95871c46d823703bd0a0979a12d2801 Mon Sep 17 00:00:00 2001 From: Time Spain Date: Wed, 28 Oct 2020 17:11:05 +0800 Subject: [PATCH 4/4] Remove the now ignored directory some-directory --- my_group,json | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 my_group,json diff --git a/my_group,json b/my_group,json deleted file mode 100644 index 5578230..0000000 --- a/my_group,json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "Jill": { - "age": 26, - "job": "biologist", - "connection": { - "Zalika": "friend", - "John": "partner" - } - }, - "Zalika": { - "age": 28, - "job": "artist", - "connection": { - "Jill": "friend" - } - }, - "John": { - "age": 27, - "job": "writer", - "connection": { - "Jill": "partner" - } - }, - "Nash": { - "age": 34, - "job": "chef", - "connection": { - "John": "cousin", - "Zalika": "landlord" - } - } -} -