-
Notifications
You must be signed in to change notification settings - Fork 64
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
sophmrtn
wants to merge
5
commits into
UCL-RITS:main
Choose a base branch
from
ArielLauu:sdm03
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ffa82d9
data structure1.0
ArielLauu 7e25e62
delete unwanted files
ArielLauu 4c5815d
Changes for issue 10 on data structures
sophmrtn 90af419
Adding code to load and save to yaml file
sophmrtn c777527
Adding VSCode settings to git ignore
sophmrtn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode/settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
print(group_dict) | ||
save_yaml(group_dict, './groupdict.yaml') | ||
print('Saving dictionary data ... ') | ||
print('Loading dictionary data ..') | ||
load_yaml('./groupdict.yaml') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍