Skip to content

Conversation

@nikshirokov
Copy link
Owner

No description provided.


print(f'Всего {len(groups)} группы.')
for i in groups:
print(f'Группа {groups.index(i) + 1}: {len(i)} ученика')
Copy link

Choose a reason for hiding this comment

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

index в list работает не супер эффективно (перебор, то есть O(n))

Если нужно одновременно обращаться и к индексу и к элементу есть два подхода:

  1. Итерируемся по индексу
    for i in len(groups):
  2. Используем enumerate что бы получать объект и индекс одноврменно:
    for index_number, group in enumerate(groups)

Вариант с enumerate может казаться менее интуитивным, но рекомендую с ним разобраться - это супер распространенная конструкция в питоне. Будет легче понимать чужой код.

# ???
student=[]
for i in groups:
print(f'Группа {groups.index(i) + 1}:',end=' ')
Copy link

Choose a reason for hiding this comment

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

тоже index

]
# ???
student=[]
for i in groups:
Copy link

Choose a reason for hiding this comment

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

Здесь и в других местах лучше использовать более self-explanatory name для iteration variable. group было бы хорошо.
Однобуквенные переменные i,j,k используем для итерирования по индексам только.

for i in groups:
print(f'Группа {groups.index(i) + 1}:',end=' ')
for j in range(len(i)):
print(i[j],end='')
Copy link

Choose a reason for hiding this comment

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

i[j] супер неожиданная (и непонятная) конструкция - пофикситься когда переименуешь.

for j in range(len(i)):
print(i[j],end='')
if j != len(i) - 1:
print(',',end='')
Copy link

Choose a reason for hiding this comment

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

Что бы упростить логику вывода можно использовать строковый метод join что бы сформировать строку и потом ее разом напечатать.
groups_as_str = ', '.join(groups)

names = dict()

for student in students:
if student['first_name'] not in names.keys():
Copy link

Choose a reason for hiding this comment

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

Suggested change
if student['first_name'] not in names.keys():
if student['first_name'] not in names:

так достаточно.

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.

4 participants