保持字典结构,同时减少嵌套字典

发布于 2025-01-13 08:27:51 字数 1468 浏览 1 评论 0原文

我有一个嵌套字典对的列表 dd ,并希望将结构维护为字典列表:

dd = [
    [{'id': 'bla',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_1A', 'amount': '2'}]},
     {'id': 'bla2',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_1B', 'amount': '1'}]}
    ],
    [{'id': 'bla3',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_2A', 'amount': '3'}]},
     {'id': 'bla4',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_2B', 'amount': '4'}]}
    ]
]

我想将其减少为配对字典列表,同时仅提取一些细节。例如,预期的输出可能如下所示:

[{'name': ['KEEP_PAIR_1A', 'KEEP_PAIR_1B'], 'amount': [2, 1]},
 {'name': ['KEEP_PAIR_2A', 'KEEP_PAIR_2B'], 'amount': [3, 4]}]

我已经运行了我的代码:

pair=[]
for all_pairs in dd:
    for output_pairs in all_pairs:
        for d in output_pairs.get('detail'):
            if d['name'] != 'discard':
                pair.append(d)
output_pair = {
    k: [d.get(k) for d in pair]
    for k in set().union(*pair)
}

但它没有维护该结构:

{'name': ['KEEP_PAIR_1A', 'KEEP_PAIR_1B', 'KEEP_PAIR_2A', 'KEEP_PAIR_2B'],
 'amount': ['2', '1', '3', '4']}
                                                                                                                       

我假设我需要使用一些列表理解来解决这个问题,但是我应该在 for 循环中的哪个位置执行此操作维持结构。

I have a list of pairs of nested dict dd and would like to maintain the structure to a list of dictionaries:

dd = [
    [{'id': 'bla',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_1A', 'amount': '2'}]},
     {'id': 'bla2',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_1B', 'amount': '1'}]}
    ],
    [{'id': 'bla3',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_2A', 'amount': '3'}]},
     {'id': 'bla4',
      'detail': [{'name': 'discard', 'amount': '123'},
                 {'name': 'KEEP_PAIR_2B', 'amount': '4'}]}
    ]
]

I want to reduce this to a list of paired dictionaries while extracting only some detail. For example, an expected output may look like this:

[{'name': ['KEEP_PAIR_1A', 'KEEP_PAIR_1B'], 'amount': [2, 1]},
 {'name': ['KEEP_PAIR_2A', 'KEEP_PAIR_2B'], 'amount': [3, 4]}]

I have run my code:

pair=[]
for all_pairs in dd:
    for output_pairs in all_pairs:
        for d in output_pairs.get('detail'):
            if d['name'] != 'discard':
                pair.append(d)
output_pair = {
    k: [d.get(k) for d in pair]
    for k in set().union(*pair)
}

But it didn't maintain that structure :

{'name': ['KEEP_PAIR_1A', 'KEEP_PAIR_1B', 'KEEP_PAIR_2A', 'KEEP_PAIR_2B'],
 'amount': ['2', '1', '3', '4']}
                                                                                                                       

I assume I would need to use some list comprehension to solve this but where in the for loop should I do that to maintain the structure.

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

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

发布评论

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

评论(1

幸福丶如此 2025-01-20 08:27:51

由于您想要组合列表中的字典,一种选择是使用 dict.setdefault:

pair = []
for all_pairs in dd:
    dct = {}
    for output_pairs in all_pairs:
        for d in output_pairs.get('detail'):
            if d['name'] != 'discard':
                for k,v in d.items():
                    dct.setdefault(k, []).append(v)
    pair.append(dct)

输出:

[{'name': ['KEEP_PAIR_1A', 'KEEP_PAIR_1B'], 'amount': [2, 1]},
 {'name': ['KEEP_PAIR_2A', 'KEEP_PAIR_2B'], 'amount': [3, 4]}]

Since you want to combine dictionaries in lists, one option is to use dict.setdefault:

pair = []
for all_pairs in dd:
    dct = {}
    for output_pairs in all_pairs:
        for d in output_pairs.get('detail'):
            if d['name'] != 'discard':
                for k,v in d.items():
                    dct.setdefault(k, []).append(v)
    pair.append(dct)

Output:

[{'name': ['KEEP_PAIR_1A', 'KEEP_PAIR_1B'], 'amount': [2, 1]},
 {'name': ['KEEP_PAIR_2A', 'KEEP_PAIR_2B'], 'amount': [3, 4]}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文