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 = 'http://winterolympicsmedals.com/medals.csv'
response = urllib2.urlopen(url)
cr = csv.reader(response)
header = next(cr)
[aList.append(tuple(row)) for row in cr]
countryList = sorted(set(item for item in aList)) # All the countries
for country in countryList:
silver = len([item for item in aList if item=='Silver' and item==country])
gold = len([item for item in aList if item=='Gold' and item==country])
bronze = len([item for item in aList if item=='Bronze' and item==country])
You could consider using a Counter.
from collections import Counter, namedtuple ... Result = namedtuple('Result', ['country','medal']) aList = [Result(country=row,medal=row) 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
swedish_bronze_count = medal_counts[("SWE", "Bronze")]