Advertisement
elena1234

Courses - sorting in Python

Jan 28th, 2022
791
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement