我对 python 和一般编程都很陌生,所以请友善。我正在尝试分析包含音乐信息的 csv 文件并返回最常听的前 n 个乐队。从下面的代码中,每听一首歌曲都是一个列表中的字典条目,格式如下:
[{'album': 'Exile on Main Street', 'song': 'Happy', 'datetime': '3 Dec 2014 14:08', 'artist': 'The Rolling Stones'}, {'album': 'II', 'song': 'Black Dog', 'datetime': '1 Dec 2014 08:08', 'artist': 'Led Zepplin'}]
from collections import Counter
def count_artist_plays(filename):
with open(filename, 'r') as data:
header = data.readline().strip().split(',')
entries = []
for line in data:
entry = line.strip().split(',')
listens = {}
for info, type in enumerate(header):
listens[type] = entry[info]
entries.append(listens)
for d in entries:
arts = d['artist']
c = Counter(arts)
print c.most_common(10)
如何获得最常见的字符串(带)而不是像下面这样的字符细分?
[('s', 2), ('a', 1), (' ', 1), ('E', 1), ('l', 1), ('o', 1), ('n', 1), ('S', 1), ('v', 1), ('y', 1)]