字符串索引必须是默认文件中CSV文件中的整数
我有.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 dorow['Brand']
in the next line, since you can access only indices inrow
, i.e.row[0]
,row[1]
, etc.