Skip to content

Yuxiang_Zhou group_dictionary operation#25

Open
zyxxyztp wants to merge 6 commits intoUCL-MPHY0021-21-22:mainfrom
zyxxyztp:Yuxiang_group_operation
Open

Yuxiang_Zhou group_dictionary operation#25
zyxxyztp wants to merge 6 commits intoUCL-MPHY0021-21-22:mainfrom
zyxxyztp:Yuxiang_group_operation

Conversation

@zyxxyztp
Copy link

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.

Solid work, though looks like you've made your life harder by pulling out who has the value for each of the questions. Can just get the values using comprehension expressions?

@@ -0,0 +1,3 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

in general, probably worth adding your .vscode directory to your gitignore

Comment on lines +4 to +10
def max_age(group_information):
max_age = 0
people = None
for key, value in group_information.items():
if max_age < value['age']:
max_age = value['age']
people = key
Copy link
Collaborator

Choose a reason for hiding this comment

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

these all do work but can you do them in a comprehension expression. So for this function we could do (note: we're not asking for who has the maximum age, just what the maximum age is)

Suggested change
def max_age(group_information):
max_age = 0
people = None
for key, value in group_information.items():
if max_age < value['age']:
max_age = value['age']
people = key
def max_age(group_information):
ages = [person["age"] for person in group_information.values()]
max_age = max(ages)

max_age = value['age']
people = key

print("The older people of this group is " + people + " and the age is " + str(max_age))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a perfect place for f strings, which let you format strings, even running code inline

Suggested change
print("The older people of this group is " + people + " and the age is " + str(max_age))
print(f"The maximum age is {max(ages)}")

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.

2 participants