Open
Conversation
n05tr0m0
reviewed
Dec 10, 2022
| if is_male[name]: | ||
| print(f'{name} мужской род') | ||
| else: | ||
| print(f'{name} женский род') |
There was a problem hiding this comment.
Можно одной строкой в данном случае:
print(name, 'мужской пол') if is_male[name] else print(name, 'женский пол')|
|
||
| students_names = count_name(students) | ||
| for name, repeats in students_names.items(): | ||
| print(f'{name}: {repeats}') |
There was a problem hiding this comment.
Решение без Counter для ознакомления:
count_students = {}
for student in students:
if student['first_name'] not in count_students.keys():
count_students[student['first_name']] = 1
else:
count_students[student['first_name']] += 1
print(count_students)
for name, count in count_students.items():
print(f"{name}: {count}")| names = count_name(students) | ||
| most_common_name = name_max_value(names) | ||
| print(f'Самое частое имя в классе: {most_common_name}') | ||
|
|
There was a problem hiding this comment.
Решение без Counter, для ознакомления:
students_list = [student['first_name'] for student in students]
print("Самое частое имя среди учеников:", max(set(students_list), key=students_list.count))| for i, school_class in enumerate(school_students): | ||
| names = count_name(school_class) | ||
| most_common_name = name_max_value(names) | ||
| print(f'Самое частое имя в классе {i+1}: {most_common_name}') |
There was a problem hiding this comment.
Переиспользование уже написанных функций хороший подход.
Публикую решение без использования Counter для ознакомления:
for num, students in enumerate(school_students, start=1):
students_list = [student['first_name'] for student in students]
print(f"Самое частое имя в классе {num}: {max(set(students_list), key=students_list.count)}")Почитай, попробуй разобраться как это работает, если не получится, напиши мне.
| for school_class in school: | ||
| gender = students_gender(school_class['students']) | ||
| class_name = school_class['class'] | ||
| print(f"Класс {class_name}: девочки {gender['Female']}, мальчики {gender['Male']}") |
There was a problem hiding this comment.
Ещё два способа решения для этой задачи для ознакомления:
for class_ in school:
count_boys = 0
count_girls = 0
for student in class_['students']:
if is_male[student['first_name']]:
count_boys += 1
else:
count_girls += 1
print(f"Класс {class_['class']}: девочки {count_girls}, мальчики {count_boys}")
print("альтернативный способ") # альтернативный способ
for class_ in school:
female = [name['first_name'] for name in class_['students'] if not is_male[name['first_name']]]
male = [name['first_name'] for name in class_['students'] if is_male[name['first_name']]]
print(f"Класс {class_['class']}: девочки {len(female)}, мальчики {len(male)}")
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.