遍历列表列表

MerynDH

我的清单如下所示:

['"date","supermarket","categoryA",10',
'"date","candy store","categoryB",5',
'"date","drugstore","categoryC",6',
'"date","supermarket","categoryA",20',
'"date","candy store","categoryB",2',
'"date","drugstore","categoryC",90']
etc

我正在尝试汇总每个类别的数字-categoryA BC等

到目前为止,已经进行了三天,主要是横盘整理。我真的应该买一本关于Python的书,因为我刚刚开始学习,现在在这里我问你们。

我知道如何在mysql中执行此操作,但是这种逻辑在这里没有帮助我。

我的代码:

for x in range(0 , len(list)):
    for y in list[x][2]:
        value += list[x][3]

扯掉我的头发,剩下的很多...

KrzysztofKrasoń

使用字典来保存聚合和迭代列表,方法是in

aggregate = {}
for x in list:
    if (x[2] not in aggregate):
        aggregate[x[2]] = 0
    aggregate[x[2]] += x[3]

以上假设您的列表列表如下所示:

[
    ["date","supermarket","categoryA",10],
    ["date","candy store","categoryB",5]
]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章