简体   繁体   English

如果 dict 与另一个“相似”,则将值附加到 dict 键

[英]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 :如果您的姓名没有排序,那么我们将需要按namedata进行排序:

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.

相关问题 如何结合两个词典? 一个字典作为关键,另一个字典作为价值 - How to combine two dictionaries? One dict as key, another dict as value 通过将一个 dict 列表的值作为键添加到另一个 dict 将两个 dict 列表组合到一个 dict - Combine two list of dict to one dict by adding value of one list of dict as key to another 一行返回列表中第一个字典中包含另一个键值的字典键的值 - one line to return value of dict key in first dict in list that contains another key value Python-添加了另一个新的dict - Python - New dict similar to another one added 将键/值从一个字典复制到另一个字典的Python方法 - Pythonic way to copy a key/value from one dict to another 将一个键和相应的值从一个字典转移到另一个 - Transfer a key and respective value from one dict to another 有没有更好的方法来查询字典中的值,键是另一个字典中的值? - Is there a better way to query value in a dict with the key which is value in another dict? 检查dict中是否至少包含一个dict键 - Check if at least one key of dict is contained in another dict 识别一个字典中的键,并使用它来修改另一个字典的值 - Identifying a key in one dict, and using it to modify values of another dict 从一个字典中检索一个键值对作为另一个字典 - Retrieve a key-value pair from a dict as another dict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM