Python Practice › Collections Programs › Program 18
Top three scorers
Write a Python program to total player runs from match records and show the top 3.
Output
[('Virat', 143), ('Rohit', 115), ('Gill', 90)]Approach
- Add runs into a Counter.
Solution
from collections import Counter innings = [("Virat", 82), ("Rohit", 45), ("Virat", 61), ("Gill", 90), ("Rohit", 70), ("Rahul", 30)]totals = Counter()for player, runs in innings: totals[player] += runsprint(totals.most_common(3))Output
[('Virat', 143), ('Rohit', 115), ('Gill', 90)]Learn the concept
New to this topic? Read the lesson, then come back.