From 1ce41b38b2e51078f658eda511ecea23c11a7902 Mon Sep 17 00:00:00 2001 From: Fudgerboy <91767657+Fudgerboy@users.noreply.github.com> Date: Mon, 20 May 2024 09:18:46 +0000 Subject: Mon, May 20, 2024, 2:18 AM -07:00 --- wk7/lect/favorites.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'wk7/lect') diff --git a/wk7/lect/favorites.py b/wk7/lect/favorites.py index 62ce6c6..fcdc97c 100644 --- a/wk7/lect/favorites.py +++ b/wk7/lect/favorites.py @@ -5,17 +5,16 @@ import csv +from collections import Counter + with open("favorites.csv") as file: reader = csv.DictReader(file) - counts = {} + counts = Counter() for row in reader: favorite = row["language"] - if favorite in counts: - counts[favorite] += 1 - else: - counts[favorite] = 1 + counts[favorite] += 1 -for favorite in sorted(counts, key=counts.get): +for favorite in sorted(counts, key=counts.get, reverse=True): print(favorite, ':', counts[favorite]) -- cgit v1.2.3