python - 使用 setdefault 重构 Python Dicts 列表

标签 python list dictionary iterable setdefault

我正在尝试通过“分组”(这可能不是正确的表达式,但将其用作基于 SQL 的代理)基于(非唯一)值的字典来重新构建 Python 字典列表。我已经接近了,但是我遇到了最后的障碍,因为我无法弄清楚如何将每个值重新分配给一个名称(即我最终得到的是一个看起来像元组而不是字典的东西)。

此外,我还有一个多余的列表(即我的输出最终为 [[{...}]] 而不是 [{...}]。

我在这里使用了示例:

How do I group this list of dicts by the same month?

这让我非常接近我想要的东西,但我还停留在最后阶段!

market = [
    {'selection_id': 1099, 'value': '11', 'value_name': 'a'},
    {'selection_id': 1099, 'value': '78', 'value_name': 'p'},
    {'selection_id': 1097, 'value': '39', 'value_name': 'b'},
    {'selection_id': 1097, 'value': '52', 'value_name': 'f'},
    {'selection_id': 1098, 'value': '98', 'value_name': 'd'},
    {'selection_id': 1099, 'value': '13', 'value_name': 'y'},
    {'selection_id': 1098, 'value': '4', 'value_name': 'r'},
]

new_structure = {}
new_structure2 = []

for z in market:
        new_structure.setdefault(z['selection_id'], []).append((z['value'], z['value_name']))
        t = [{'selection_id': m, 'value_dict': n} for m, n in new_structure.items()]
new_structure2.append(t)

print(new_structure2)

这输出为:

[[{'selection_id': 1099, 'value_dict': [('11', 'a'), ('78', 'p'), ('13',  
 'y')]}, {'selection_id': 1097, 'value_dict': [('39', 'b'), ('52', 'f')]},  
 {'selection_id': 1098, 'value_dict': [('98', 'd'), ('4', 'r')]}]]

这非常接近,但我的目标是:

[{'selection_id': 1099,
  'value_dict': [{'value': '11', 'value_name': 'a'},
                 {'value': '78', 'value_name': 'p'},
                 {'value': '13', 'value_name': 'y'}]},
 {'selection_id': 1097,
  'value_dict': [{'value': '39', 'value_name': 'b'},
                 {'value': '52', 'value_name': 'f'}]},
 {'selection_id': 1098,
  'value_dict': [{'value': '98', 'value_name': 'd'},
                 {'value': '4', 'value_name': 'r'}]}]

我很感激它可能是一个非常简单的修复程序,但它现在正在逃避我,所以任何指导将不胜感激!

最佳答案

这里有一些提示:

首先是按selection_id排序:

by_selection_id = operator.itemgetter('selection_id')
market.sort(key=by_selection_id)

然后你可以按selection_id分组:

for selection_id, group in itertools.groupby(market, key=by_selection_id):
    print(selection_id, list(group))

你得到:

(1097, [{'value_name': 'b', 'value': '39', 'selection_id': 1097},
        {'value_name': 'f', 'value': '52', 'selection_id': 1097}])
(1098, [{'value_name': 'd', 'value': '98', 'selection_id': 1098},
        {'value_name': 'r', 'value': '4', 'selection_id': 1098}])
(1099, [{'value_name': 'a', 'value': '11', 'selection_id': 1099},
        {'value_name': 'p', 'value': '78', 'selection_id': 1099},
        {'value_name': 'y', 'value': '13', 'selection_id': 1099}])

然后很容易构建最终列表。

这是一个使用理解列表/字典的解决方案:

new_structure = [{'selection_id': selection_id,
                  'value_dict': [{'value': item['value'],
                                  'value_name': item['value_name']} for item in group]}
                 for selection_id, group in itertools.groupby(market, key=by_selection_id)]

或者使用带有 append 的经典列表:

new_structure = []
for selection_id, group in itertools.groupby(market, key=by_selection_id):
    value_dict = [{'value': item['value'], 'value_name': item['value_name']} for item in group]
    new_structure.append({'selection_id': selection_id,
                          'value_dict': value_dict})

关于python - 使用 setdefault 重构 Python Dicts 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56156089/

相关文章:

vb.net - IXmlSerializable 字典问题

python - 删除带零的行

python - 使用 for 循环将 header 添加到乘法表

python-3.x - Python 列表创建差异

android - 如何使列表 <enum> 可打包?

python - Go 中的字典

python - jupyter lab 中的缩进自动换行是否可行?

python - 抓取 : How to fetch an attribute in a <abbr> tag

c# - 如何在 List<T>.RemoveAll 方法不删除任何内容时抛出异常?

C++ Map双线程并发插入读取