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

Issue10 - comprehension expressions #82

Open
wants to merge 3 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
75 changes: 74 additions & 1 deletion group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,77 @@

# Your code to go here...

my_group =
import json

my_group = {

"Jill" :

{'job': 'Biologist' ,
'age': '26',
'relations': {'Zalika': 'friend', 'John': 'Partner'}},

"Zalika" :

{'job': 'artist' ,
'age': '28',
'relations': {'Jill': 'friend'}},

"John" :

{'job': 'writer' ,
'age': '27',
'relations': {'Jill': 'Partner'}},

"Nash" :

{'job': 'chef' ,
'age': '34',
'relations': {'Zalika': 'landlord', 'John': 'cousin'}},

"Richard" :

{'job': 'writer' ,
'age': '53',
'relations': {}}


}


#some examples of how to retreive info from dict
print(f"Zalika's age is: {my_group['Zalika']['age']}")
print(f"Nash's job is: {my_group['Nash']['job']}")
print(f"Relationship between Jill and John is: {my_group['Jill']['relations']['John']}")

#return max age of group
ages = [my_group[i]['age'] for i in my_group.keys()]
print(f"The maximum age of this group of people is: {max(ages)}")

#return mean number of relations in group
rels = [len(my_group[i]['relations']) for i in my_group.keys()]
print(f"The mean number of relations in this group of people is: {sum(rels)/len(rels)}")

#return max age of group that have at least one relation
ages_rels = [my_group[i]['age'] for i in my_group.keys() if len(my_group[i]['relations']) >= 1]
print(f"The maximum age of people in this group that have at lease one relation is: {max(ages_rels)}")

#return max age of group that have at least one friend
ages_friends = [my_group[i]['age'] for i in my_group.keys() if 'friend' in my_group[i]['relations'].values()]
print(f"The maximum age of people in this group that have at lease one friend is: {max(ages_friends)}")



#JSON

#write
with open('groupdata.json' ,'w') as jsonfile:
json.dump(my_group, jsonfile, indent=3)

#load
with open('groupdata.json' ,'r') as loadedjsonfile:
json.load(loadedjsonfile)
print(loadedjsonfile)



37 changes: 37 additions & 0 deletions groupdata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"Jill": {
"job": "Biologist",
"age": "26",
"relations": {
"Zalika": "friend",
"John": "Partner"
}
},
"Zalika": {
"job": "artist",
"age": "28",
"relations": {
"Jill": "friend"
}
},
"John": {
"job": "writer",
"age": "27",
"relations": {
"Jill": "Partner"
}
},
"Nash": {
"job": "chef",
"age": "34",
"relations": {
"Zalika": "landlord",
"John": "cousin"
}
},
"Richard": {
"job": "writer",
"age": "53",
"relations": {}
}
}