我有一个带有音乐家ssn的csv文件和他们写的专辑,我需要数一数每个音乐家写了多少张专辑
CSV文件如下所示:
100000000,7
100000000,21
100000000,24
100000001,5
100000001,7
100000001,16
100000002,9
100000002,14
100000002,15
100000002,21
100000003,2
100000003,8
100000003,10
100000003,14
100000003,15
100000003,19我的代码需要输出如下内容:
100000000 no of albums = 3
100000001 no of albums = 3
100000002 no of albums = 4
100000003 no of albums = 6发布于 2021-11-30 05:01:45
with open("test.csv", "r") as f:
count = dict()
for line in f.readlines():
artist = line.split(",")[0]
if artist in count.keys():
count[artist] += 1
else:
count[artist] = 1
print(count)这是一个简单的代码片段,可以完成这项工作。这不是最优雅的,但你会明白逻辑的。
https://stackoverflow.com/questions/70164479
复制相似问题