我正在尝试读取csv文件并尝试编写逻辑以便以特定格式打印出来。我已经完成了如下操作,我想问你是否可以做得更好。 我的逻辑如下:
import csv, urllib2
url = 'http://winterolympicsmedals.com/medals.csv'
...
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])
result.append((country,gold,silver,bronze))
result
谢谢,我非常感谢您的建议。 :)
答案 0 :(得分:2)
您可以考虑使用计数器。
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)
然后根据需要对结果进行排序和打印。
这将给出(country, medal)
的出现次数,您应该可以轻松解析它以给出最终列表。
请注意,您可以像访问Counter
:
dict
swedish_bronze_count = medal_counts[("SWE", "Bronze")]
答案 1 :(得分:1)
您正在使用列表推导来附加到另一个列表。列表推导生成列表,因此您可以替换:
aList =[]
[aList.append(tuple(row)) for row in cr]
与
aList = [tuple(row) for row in cr]
答案 2 :(得分:0)
这将为您提供您正在寻找的结果的数据框
import pandas as pd
df = pd.read_csv('http://winterolympicsmedals.com/medals.csv')
counts = df.groupby('NOC')['Medal'].value_counts().unstack()
或者,如果您只想使用标准库:
import csv, urllib, io
from collections import defaultdict
url = 'http://winterolympicsmedals.com/medals.csv'
webpage = urllib.request.urlopen(url)
datareader = csv.DictReader(io.TextIOWrapper(webpage))
counts = defaultdict(lambda: defaultdict(int))
for line in datareader:
country = line['NOC']
medal = line['Medal']
counts[country][medal] += 1
counts['USA']
输出
defaultdict(int, {'Bronze': 58, 'Gold': 78, 'Silver': 80})
你想要的最终格式
res = [(country, v['Gold'], v['Silver'], v['Bronze']) for country,v in counts.items()]
res[0:5]
输出
[('PRK', 0, 1, 1),
('GDR', 39, 36, 35),
('EST', 4, 1, 1),
('AUT', 51, 64, 70),
('AUS', 3, 0, 3)]