[英]Appending value to dict key if dict is “similar” to another one
First of all, sorry for the title, I couldn't think of a good one to be honest.首先,对不起标题,老实说,我想不出一个好的标题。
I have a list of dictionaries like this我有一个这样的字典列表
data=[
{'identifier': 'ID', 'resource': 'resource1' , 'name': 'name1'},
{'identifier': 'ID', 'resource': 'resource2' , 'name': 'name1'},
{'identifier': 'ID', 'resource': 'resource3' , 'name': 'name1'},
{'identifier': 'ID', 'resource': 'resource1' , 'name': 'name2'},
{'identifier': 'ID', 'resource': 'resource2' , 'name': 'name2'},
{'identifier': 'ID', 'resource': 'resource3' , 'name': 'name2'}
]
Basically, I want a dict that contains the name and every resource with that name, something like this基本上,我想要一个包含名称和具有该名称的每个资源的字典,就像这样
final = [
{
'name': 'name1',
'resources': ['resource1','resource2','resource3']
},
{
'name': 'name2',
'resources': ['resource1','resource2','resource3']
}
]
I have tried some approaches, like iterating over the first list and verifying if the key value
pair already exists on the second one, so after that I can append the resource to the key I want but clearly thats not the correct way to do it.我尝试了一些方法,例如遍历第一个列表并验证第二个列表中是否已经存在
key value
对,因此之后我可以将资源 append 分配给我想要的键,但显然这不是正确的方法。
Im sure there's a way to easily do this but I cant wrap my head around on how to achieve it.我确定有一种方法可以轻松做到这一点,但我无法解决如何实现它。 Any ideas on how can this be done?
关于如何做到这一点的任何想法?
Group with a collections.defaultdict
, with name
as the grouping key and the resources for each name appended to a list:使用
collections.defaultdict
进行分组, name
作为分组键,每个名称的资源附加到列表中:
from collections import defaultdict
data = [
{"identifier": "ID", "resource": "resource1", "name": "name1"},
{"identifier": "ID", "resource": "resource2", "name": "name1"},
{"identifier": "ID", "resource": "resource3", "name": "name1"},
{"identifier": "ID", "resource": "resource1", "name": "name2"},
{"identifier": "ID", "resource": "resource2", "name": "name2"},
{"identifier": "ID", "resource": "resource3", "name": "name2"},
]
d = defaultdict(list)
for x in data:
d[x["name"]].append(x["resource"])
result = [{"name": k, "resources": v} for k, v in d.items()]
print(result)
However, since your names are ordered, we can also get away with using itertools.groupby
:但是,由于您的名字是有序的,我们也可以使用
itertools.groupby
:
from itertools import groupby
from operator import itemgetter
result = [
{"name": k, "resources": [x["resource"] for x in g]}
for k, g in groupby(data, key=itemgetter("name"))
]
print(result)
If your names are not ordered, then we will need to sort data
by name
:如果您的姓名没有排序,那么我们将需要按
name
对data
进行排序:
result = [
{"name": k, "resources": [x["resource"] for x in g]}
for k, g in groupby(sorted(data, key=itemgetter("name")), key=itemgetter("name"))
]
print(result)
Output: Output:
[{'name': 'name1', 'resources': ['resource1', 'resource2', 'resource3']}, {'name': 'name2', 'resources': ['resource1', 'resource2', 'resource3']}]
Note: I would probably just stick with the first defaultdict
solution in most cases, because it doesn't care about order.注意:在大多数情况下,我可能会坚持使用第一个
defaultdict
解决方案,因为它不关心顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.