user6384905 user6384905 - 1 year ago 89
Python Question

efficient counting/sorting with lists and csv files

I am trying to read a csv file winter olympics and trying to write logic for printing out in the format [ ('country1', '# of gold', '# of silver', '# of bronze'), ('country2', '# of gold', '# of silver', '# of bronze'),....]
I have done it as below and i wish to ask you if I could have done it better.
My logic below:

import csv, urllib2
url = ''
response = urllib2.urlopen(url)
cr = csv.reader(response)
header = next(cr)
aList =[]
[aList.append(tuple(row)) for row in cr]
countryList = sorted(set(item[4] for item in aList)) # All the countries
for country in countryList:
silver = len([item for item in aList if item[7]=='Silver' and item[4]==country])
gold = len([item for item in aList if item[7]=='Gold' and item[4]==country])
bronze = len([item for item in aList if item[7]=='Bronze' and item[4]==country])

Thanks, I Greatly appreciate your advice. :)

Answer Source

You could consider using a Counter.

from collections import Counter, namedtuple

Result = namedtuple('Result', ['country','medal'])
aList = [Result(country=row[4],medal=row[7]) for row in cr]
medal_counts = Counter(alist)

And then sort and print out the results however you desire.

This will give a tally of the occurences of (country, medal), which you should be able to easily parse to give a final list.

Note that you can access a Counter as you would a dict:

swedish_bronze_count = medal_counts[("SWE", "Bronze")]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download