Files
courses/py3interm/EXAMPLES/count_with_dict.py
T
2025-05-20 11:57:43 -04:00

14 lines
404 B
Python

#!/usr/bin/env python
counts = {} # <1>
with open("../DATA/breakfast.txt") as breakfast_in:
for line in breakfast_in:
breakfast_item = line.rstrip('\n\r')
if breakfast_item in counts: # <2>
counts[breakfast_item] = counts[breakfast_item] + 1 # <3>
else:
counts[breakfast_item] = 1 # <4>
for item, count in counts.items():
print(item, count)