我在使用此代码时遇到一些问题。我应该从每年(1965-2015)的前10首歌曲中检索20个最常见的单词。有一个排名,所以我觉得我可以用排名来识别前10名<;=10但我对如何开始感到迷茫。这就是我目前所拥有的。我还没有收录排名前十的歌曲。另外,最常见的20个单词来自歌词栏(第4栏)
import collections
import csv
import re
words = re.findall(r'\w+', open('billboard_songs.csv').read().lower())
reader = csv.reader(words, delimiter=',')
csvrow = [row[4] for row in reader]
most_common = collections.Counter(words[4]).most_common(20)
print(most_common)
我的文件的第一行如下所示:
"Rank","Song","Artist","Year","Lyrics","Source"
1,"wooly bully","sam the sham and the pharaohs",1965,"sam the sham miscellaneous wooly bully wooly bully sam the sham the pharaohs .....,3
当它达到100(排名)时,第二年又从1开始,以此类推
您可以使用
csv.DictReader
来解析文件,并从中获得可用的Python词典列表。然后,您可以使用for comprehensions和itertools.groupby()
提取所需的歌曲信息。最后,您可以使用collections.Counter
查找歌曲中最常见的单词在这个回答中,我假设
billboard_songs.csv
的格式如下:我假设数据集是1965年到2015年,如问题中所述。如果没有,则应首先相应地过滤歌曲列表
相关问题 更多 >
编程相关推荐