Skip to content

Using comprehensions#18

Open
CoreyFulop wants to merge 2 commits intoUCL-MPHY0021-21-22:mainfrom
howardys:coreyfulop
Open

Using comprehensions#18
CoreyFulop wants to merge 2 commits intoUCL-MPHY0021-21-22:mainfrom
howardys:coreyfulop

Conversation

@CoreyFulop
Copy link

I hope this works!

Answers UCL-MPHY0021-21-22/RSE-Classwork#12

Copy link
Collaborator

@stefpiatek stefpiatek left a comment

Choose a reason for hiding this comment

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

Different way to model the relationships, added in some suggestions/comments

Comment on lines +8 to +12
"friend": "zalika",
"partner": "john",
"cousin": "",
"landlord": "",
"tenant": ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

ah interesting, instead of a nested relationships you've got each one as an attribute. It works but I guess you end up having a lot of empty values!

ages = [my_group[person]["age"] for person in my_group]

# Assuming relations means has partner or cousin
partners = ["partner" for person in my_group if my_group[person]["partner"]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you simplify this to

Suggested change
partners = ["partner" for person in my_group if my_group[person]["partner"]]
partners = ["partner" for person in my_group if person["partner"]]

Comment on lines +47 to +51
partners = ["partner" for person in my_group if my_group[person]["partner"]]
cousins = ["cousin" for person in my_group if my_group[person]["cousin"]]

relations = []
relations.extend(partners + cousins)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also could you do it all in one expression?

Suggested change
partners = ["partner" for person in my_group if my_group[person]["partner"]]
cousins = ["cousin" for person in my_group if my_group[person]["cousin"]]
relations = []
relations.extend(partners + cousins)
partners = ["partner" for person in my_group if my_group[person]["partner"]]
cousins = ["cousin" for person in my_group if my_group[person]["cousin"]]
relations = []
relations.extend(partners + cousins)

has_friends = [my_group[person]["age"] for person in my_group if my_group[person]["friend"]]

# Print results
print(f"The maximum age in our group is: {max(ages)}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice use of f strings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants