3 ms·
The groupby, if done using itertools, will cut some code. from itertools import groupby customer_data = (line.strip().split(',') for line in file('cus
by irahul 16y ago
The groupby, if done using itertools, will cut some code.
from itertools import groupby
customer_data = (line.strip().split(',') for line in file('customer.txt'))
grouped_ages = groupby(customer_data, key=itemgetter(1))
print sorted((len(list(g)), k) for k, g in grouped_ages)[-3:]
- anthonyb 16y agoAh yes, I'd forgotten about groupby. You'll also need to do a from operator import itemgetter as well.