试图让python函数返回频率表

问题描述

opened_file = open(r'C:\Users\lalus\Desktop\Datasets\cars.csv')
from csv import reader
read_file = reader(opened_file)
cars_data = list(read_file)

def howMany(index):
    a_list = {}

    for row in cars_data[1:]:
        if row[index] in a_list:
           a_list[index] += 1
        else:
            a_list[index] = 1

return a_list

make_count = howMany(2)

print(make_count)

我的 csv 文件在索引 2 处有一行是一系列汽车的品牌。我希望输出返回一个汽车品牌列表以及该品牌在列中出现的次数

到目前为止,此代码仅返回 {2:1}。

我错过了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)