字符串索引必须是默认文件中CSV文件中的整数

发布于 2025-01-25 15:38:29 字数 403 浏览 3 评论 0原文

我有.csv文件,关于拉面和品牌,品种和评分。我想弄清楚,哪个品牌最多使用的“汤姆百胜”。我尝试使用违约行为,但是我得到了错误代码:字符串索引必须是整数

这是我到目前为止的代码:

from collections import defaultdict
tomyum = []

for row in liste:
    if "Tom Yum" in row["Variety"]:
        tomyum.append(row["Brand"])
        


d = defaultdict(int)

for row in tomyum:
    for brand in row['Brand']:
        d[brand] += 1
d   

有人有任何想法吗?

I have .csv file, about ramen and brands, varieties and ratings. I want to figure out, which Brand uses the Variety "Tom Yum" the most. I tried it with a defaultdict but i get the error code: string indices must be integers

This is my code so far:

from collections import defaultdict
tomyum = []

for row in liste:
    if "Tom Yum" in row["Variety"]:
        tomyum.append(row["Brand"])
        


d = defaultdict(int)

for row in tomyum:
    for brand in row['Brand']:
        d[brand] += 1
d   

Anyone any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2025-02-01 15:38:29

Tomyum是字符串列表。因此,在以下循环中:tomyum中的行,row表示字符串。这就是为什么您不能在下一行中执行row ['brand'],因为您只能在row中仅访问索引,即>,<代码>行[1] ,等等。

tomyum is a list of strings. Hence, in the following loop: for row in tomyum, row represents a string. That's why you cannot do row['Brand'] in the next line, since you can access only indices in row, i.e. row[0], row[1], etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文