使用setdefault重构Python字典列表 [英] Re-structuring a list of Python Dicts using setdefault
问题描述
我试图通过分组"(可能不是正确的表达式,但是将其用作基于SQL的代理)基于(非唯一)值的字典来重新构建Python字典列表.我已经接近了,但是我遇到了最后的障碍,因为我无法解决如何将每个值重新分配给一个名称(即,我得到的看起来像是元组而不是字典).
I am trying to re-structure a list of Python dictionaries by 'grouping' (that's probably not the correct expression, but using it as a proxy based on SQL) the dictionaries based on a (non-unique) value. I have got close, however I'm falling at the final hurdle, in that I couldn't work out how to re-assign each value to a name (i.e. I end up with what looks like a tuple rather than a dict).
另外,我还有一个多余的列表(即,我的输出最终以[[{...}]]而不是[{......]]来显示.
Additionally I have a superfluous list (i.e. my output ends up as [[{...}]] rather than [{...}].
我在这里使用了示例:
这让我非常接近我想要的东西,但是我被困在最后阶段!
Which gets me quite close to what I want, however I'm stuck at the final stage!
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')]}]]
哪个位置很近,但是我的目标是:
Which is very close, however what I'm aiming for is:
[{'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'}]}]
我很感激它可能是一个非常简单的修复程序,但此刻它使我逃脱了,因此任何指导将不胜感激!
I appreciate its probably a really simple fix, but its escaping me at the moment, so any guidance would be greatly appreciated!
推荐答案
以下是一些提示:
第一件事是按 selection_id 进行排序:
The first thing is to sort by 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}])
然后很容易建立最终列表.
Then it is easy to build the final list.
这是使用理解列表/字典的解决方案:
Here is a solution using comprehension list/dict:
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})
这篇关于使用setdefault重构Python字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!