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

exercise in json #73

Open
wants to merge 1 commit 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
89 changes: 87 additions & 2 deletions group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,90 @@
"""An example of how to represent a group of acquaintances in Python."""
import json

# Your code to go here...
my_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"
}
}
}

my_group =
#the maximum age of people in the group

max_age_group = []

for groupname,groupinfo in my_group.items():
max_age_group.append(groupinfo['age'])

print('1- Maximum age of people in group is: ', end="")
print(max(max_age_group))

#the average (mean) number of relations among members of the group

ave_relation_group = []

for groupname,groupinfo in my_group.items():
relation_nb = len(groupinfo['relations'].values())
ave_relation_group.append(relation_nb)

print('2- Average (mean) number of relations among members of the group is: ', end="")
print(max(ave_relation_group))

# the maximum age of people in the group that have at least one relation

max_age_one_relation = []

for groupname,groupinfo in my_group.items():
if len(groupinfo['relations'].values()) >= 1:
max_age_one_relation.append(groupinfo['age'])

print('3- Maximum age of people in the group that have at least one relation is: ', end="")
print(max(max_age_one_relation))


# [more advanced] the maximum age of people in the group that have at least one friend

max_age_one_friend = []

for groupname,groupinfo in my_group.items():
for relation in groupinfo['relations'].values() :
if relation == 'friend':
max_age_one_friend.append(groupinfo['age'])

print('4- Maximum age of people in the group that have at least one friend is: ', end="")
print(max(max_age_one_friend))

#Reading and writing structured data files
filename = 'my_group.json'
with open(filename,'w') as f:
json.dump(my_group, f)

with open(filename) as f:
group_info = json.load(f)

print(group_info)
1 change: 1 addition & 0 deletions my_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"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"}}}