简体   繁体   English

使用 for 循环遍历字典列表

[英]iterating over a list of dictionaries with a for loop

在此处输入图像描述 I have a variable that looks like this, it contains multiple lists and each list has multiple dictionaries.我有一个看起来像这样的变量,它包含多个列表,每个列表都有多个字典。 what i need to do now is:我现在需要做的是:

  1. combine the lists into 1 big list将列表合并为 1 个大列表
  2. if 2 dictionaries have the same key i need to combine them(keep 1 of the keys and add their values)如果 2 个字典具有相同的键,我需要将它们组合起来(保留 1 个键并添加它们的值)

i know i need to use a for loop but how do i reference dictionaries inside a list and how do i refernce the lists stored in the variable?我知道我需要使用 for 循环,但如何在列表中引用字典以及如何引用存储在变量中的列表?

i tried doing something like this:我试着做这样的事情:

        for list in bigram_lists:
            for list1 in bigram_lists:
                list.append(list1)

it gives back the error that dict object has no attribute append help would be appreciated它返回 dict object 没有属性的错误 append 帮助将不胜感激

import ast

x = "[{'a': 1850}, {'b': 397}, {'c': 811}, {'d': 990}, {'e': 3198}, {'f': 605}, {'g': 435}, {'h': 1339}, {'i': 1904}, {'j': 59}, {'k': 138}, {'l': 946}, {'m': 652}, {'n': 1691}, {'o': 1813}, {'p': 510}, {'q': 13}, {'r': 1469}, {'s': 1695}, {'t': 2322}, {'u': 516}, {'v': 285}, {'w': 353}, {'x': 49}, {'y': 393}, {'z': 23}] [{'a': 3815}, {'b': 716}, {'c': 1989}, {'d': 1904}, {'e': 5429}, {'f': 908}, {'g': 836}, {'h': 1902}, {'i': 3340}, {'j': 42}, {'k': 148}, {'l': 1818}, {'m': 1156}, {'n': 3782}, {'o': 3365}, {'p': 992}, {'q': 98}, {'r': 2683}, {'s': 3125}, {'t': 3708}, {'u': 1123}, {'v': 335}, {'w': 399}, {'x': 153}, {'y': 706}, {'z': 85}] [{'a': 5087}, {'b': 823}, {'c': 1949}, {'d': 2366}, {'e': 6904}, {'f': 1322}, {'g': 1128}, {'h': 2756}, {'i': 3754}, {'j': 138}, {'k': 346}, {'l': 2709}, {'m': 1618}, {'n': 4391}, {'o': 4675}, {'p': 1321}, {'q': 74}, {'r': 3681}, {'s': 3554}, {'t': 5438}, {'u': 1658}, {'v': 519}, {'w': 1012}, {'x': 128}, {'y': 718}, {'z': 53}]"

strs = x.replace(']','],')[:-1]
strs = "[" + strs + "]"
listOfLists = ast.literal_eval(strs)

finalDict = {}
for ls in listOfLists:
    for dct in ls:
        if (list(dct.keys())[0]) in finalDict:
            finalDict[list(dct.keys())[0]] += dct[list(dct.keys())[0]]
        else:
            finalDict[list(dct.keys())[0]] = dct[list(dct.keys())[0]]

print(finalDict)

gives you给你

{'a': 10752, 'b': 1936, 'c': 4749, 'd': 5260, 'e': 15531, 'f': 2835, 'g': 2399, 'h': 5997, 'i': 8998, 'j': 239, 'k': 632, 'l': 5473, 'm': 3426, 'n': 9864, 'o': 9853, 'p': 2823, 'q': 185, 'r': 7833, 's': 8374, 't': 11468, 'u': 3297, 'v': 1139, 'w': 1764, 'x': 330, 'y': 1817, 'z': 161}

Working with x as a list of lists, I created a dictionary with multiple keys, that you can split if you want later, but each key has the addition of the same key in each list:使用x作为列表列表,我创建了一个包含多个键的字典,如果以后需要可以拆分,但每个键在每个列表中都添加了相同的键:

result = {}
for sublist in x:
    for elem in sublist:
        for key, value in elem.items():
            if key not in result:
                result[key] = value
            else:
                result[key] += value
>>> print(result)
{'a': 10752, 'b': 1936, 'c': 4749, 'd': 5260, 'e': 15531, 'f': 2835, 'g': 2399, 'h': 5997, 'i': 8998, 'j': 239, 'k': 632, 'l': 5473, 'm': 3426, 'n': 9864, 'o': 9853, 'p': 2823, 'q': 185, 'r': 7833, 's': 8374, 't': 11468, 'u': 3297, 'v': 1139, 'w': 1764, 'x': 330, 'y': 1817, 'z': 161}

Having corrected the x input as a list of lists:将 x 输入更正为列表列表:

x = [[{'a': 1850}, {'b': 397}, {'c': 811}, {'d': 990}, {'e': 
3198}, {'f': 605}, {'g': 435}, {'h': 1339}, {'i': 1904}, {'j': 
59}, {'k': 138}, {'l': 946}, {'m': 652}, {'n': 1691}, {'o': 
1813}, {'p': 510}, {'q': 13}, {'r': 1469}, {'s': 1695}, {'t': 
2322}, {'u': 516}, {'v': 285}, {'w': 353}, {'x': 49}, {'y': 393}, 
{'z': 23}],

[{'a': 3815}, {'b': 716}, {'c': 1989}, {'d': 1904}, {'e': 5429}, 
{'f': 908}, {'g': 836}, {'h': 1902}, {'i': 3340}, {'j': 42}, 
{'k': 148}, {'l': 1818}, {'m': 1156}, {'n': 3782}, {'o': 3365}, 
{'p': 992}, {'q': 98}, {'r': 2683}, {'s': 3125}, {'t': 3708}, 
{'u': 1123}, {'v': 335}, {'w': 399}, {'x': 153}, {'y': 706}, 
{'z': 85}],

[{'a': 5087}, {'b': 823}, {'c': 1949}, {'d': 2366}, {'e': 6904}, 
{'f': 1322}, {'g': 1128}, {'h': 2756}, {'i': 3754}, {'j': 138}, 
{'k': 346}, {'l': 2709}, {'m': 1618}, {'n': 4391}, {'o': 4675}, 
{'p': 1321}, {'q': 74}, {'r': 3681}, {'s': 3554}, {'t': 5438}, 
{'u': 1658}, {'v': 519}, {'w': 1012}, {'x': 128}, {'y': 718}, 
{'z': 53}]]

this:这个:

R=[]
for ld in x:
    result = {}
    for d in ld:
        result.update(d)
    R.append(result)

D = dict.fromkeys(R[0].keys(), 0)

for d in R:
    for k in R[0].keys():
        D[k]+=d[k]

will give you the answer you wanted.会给你你想要的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM