Advertisement
elena1234

Courses - sorting in Python

Jan 28th, 2022
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. university = {}
  2. line = input().split(" : ")
  3. while line != ["end"]:
  4.     course = line[0]
  5.     student = line[1]
  6.     if course not in university:
  7.         university[course] = []
  8.         university[course].append(student)
  9.     else:
  10.         university[course].append(student)
  11.    
  12.     line = input().split(" : ")
  13.  
  14. sorted_univeristy = dict(sorted(university.items(), key = lambda x: -len(x[1])))
  15. for course_name, students in sorted_univeristy.items():
  16.     print (f"{course_name}: {len(students)}")
  17.     sorted_students = sorted(students)
  18.     for student in sorted_students:
  19.         print(f"-- {student}")
  20.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement