Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing group dict and writing main function to retrieve data #45

Open
wants to merge 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/settings.json
57 changes: 54 additions & 3 deletions group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
"""An example of how to represent a group of acquaintances in Python."""
# - Jill is 26, a biologist and she is Zalika's friend and John's partner.
# - Zalika is 28, an artist, and Jill's friend
# - John is 27, a writer, and Jill's partner.
# - Nash is 34, a chef, John's cousin and Zalika's landlord.

# Your code to go here...
group_dict={'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 =

def load_yaml(filepath):
# Prints yaml data from file provided
import yaml

with open(filepath, 'r') as myfile:
data = yaml.safe_load(myfile)
print(data)


def save_yaml(mydict, filepath):
# Saves dictionary data to yaml file in location specified
import yaml

with open(filepath, 'w') as myfile:
yaml.dump(mydict, myfile)

def main():
# Collect data
data = group_dict.values()

# 1. Calculate maximum age in the group
maximum_age = max([x['age'] for x in data])
print('The maximum age in group_dict is :', maximum_age)

# 2. Average number of relations in the group
import numpy as np
av_connections = np.mean([len(x['connection']) for x in data])
print('The average number of connections in group_dict is :', av_connections)

#3. The maximum age of people with at least one relation
maximum_age = max([x['age'] for x in data if len(x['connection']) >= 1])
print('The maximum age of people in group_dict with at least one connection is :', maximum_age)

#4. The maximum age of people in the group that have at least one friend
maximum_age = max([x['age'] for x in data if len([connection for connection in x['connection'].values() if connection=='friend']) >=1 ])
print('The maximum age of people in group_dict with at least one friend is :', maximum_age)
Comment on lines +30 to +47
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍




if __name__ == "__main__":
print(group_dict)
save_yaml(group_dict, './groupdict.yaml')
print('Saving dictionary data ... ')
print('Loading dictionary data ..')
load_yaml('./groupdict.yaml')
22 changes: 22 additions & 0 deletions groupdict.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Jill:
age: 26
connection:
John: partner
Zalika: friend
job: biologist
John:
age: 27
connection:
Jill: partner
job: writer
Nash:
age: 34
connection:
John: cousin
Zalika: landlord
job: chef
Zalika:
age: 28
connection:
Jill: friend
job: artist