Skip to content

Instantly share code, notes, and snippets.

@mikedigriz
Last active May 3, 2023 04:19
Show Gist options
  • Save mikedigriz/99c3671c1a52d5d545854de9e9a4beb9 to your computer and use it in GitHub Desktop.
Save mikedigriz/99c3671c1a52d5d545854de9e9a4beb9 to your computer and use it in GitHub Desktop.
get_top_by_key_from_csv
Timestamp language problem
10/24/2022 8:33:26 C Credit
10/24/2022 10:32:26 Python Runoff
10/24/2022 11:10:47 Python Mario
10/24/2022 11:22:35 Python Scratch
10/24/2022 11:39:06 Python Readability
10/24/2022 11:53:00 Scratch Scratch
10/24/2022 13:26:23 C Bulbs
10/24/2022 13:32:09 Python Filter
10/24/2022 13:36:35 Python DNA
10/24/2022 13:37:20 Scratch Scratch
10/24/2022 13:37:22 Scratch Scratch
10/24/2022 13:37:23 Python Hello
10/24/2022 13:37:24 Python DNA
10/24/2022 13:37:25 Python Hello
10/24/2022 13:37:26 Scratch Cash
10/24/2022 13:37:28 Python Readability
10/24/2022 13:37:29 Scratch Scratch
10/24/2022 13:37:30 Python DNA
10/24/2022 13:37:30 C Speller
10/24/2022 13:37:30 Python Mario
10/24/2022 13:37:30 Scratch Filter
10/24/2022 13:37:30 Python Readability
10/24/2022 13:37:30 Python Filter
# SELECT problem, COUNT(*) FROM favorites GROUP BY problem ORDER BY COUNT(*) DESC;
# INSERT INTO favorites (language, problem) VALUES('SQL', 'bOOM');
# UPDATE favorites SET Timestamp = 'NONE' WHERE language = 'SQL' AND problem = 'bOOM';
# DELETE FROM favorites WHERE language = 'SQL' AND problem = 'bOOM';
# Sorts favorites by value using lambda function
import csv
# Open CSV file
with open("dict_ex.csv", "r") as file:
# Create DictReader
reader = csv.DictReader(file)
# Counts
counts = {}
# Iterate over CSV file, counting favorites
for row in reader:
favorite = row["problem"]
if favorite in counts:
counts[favorite] += 1
else:
counts[favorite] = 1
# Print counts
for favorite in sorted(counts, key=lambda problem: counts[problem], reverse=True):
print(f"{favorite}: {counts[favorite]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment