Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Exercise for week3 #59

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,68 @@

# Your code to go here...

my_group =
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"
}
}
}


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'].values()}
print("The maximum age of people in the group that have at least one friend is:", max(age_3.values()))
Comment on lines +44 to +57
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 might be easier to just create a list comprehension so you don't need to keep on calling .values() for the dict


# 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)
33 changes: 33 additions & 0 deletions my_group.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}