简体   繁体   English

将词典列表转换为唯一的词典列表

[英]Converting list of dictionaries to unique list of dictionaries

I have been struggling for hours to solve the following issue without success. 我一直在努力解决以下问题而没有成功。

I have a data structure that looks like this: 我有一个如下所示的数据结构:

[   {   'ROOT': [   
            {   'firstElem': 'gc-3/1/0'},
            {   'SecondElem': '5.0.0.1'},
            {   'ThirdElem': '127.3.15.1'},
            {   'index': 16},
            {   'function': 'session'},
            {   'hw': '0.0.0.0'},
            {   'sw': '1.50.1.3'},
            {   'resources': [   {   'cpu-info': [   {   'cpu-peak-load': 1},
                                                     {   'cpu-avg-load': 1}]},
                                 {   'memory-total': 1},
                                 {   'memory-used': 2}]},
            ]},
    {   'ROOT': [
            {   'firstElem': 'gc-4/1/0'},
            {   'SecondElem': '5.0.0.2'},
            {   'ThirdElem': '127.3.4.1'},
            {   'index': 5},
            {   'function': 'stand'},
            {   'hw': '0.0.0.0'},
            {   'sw': '1.50.1.3'},
            {   'resources': [   {   'cpu-info': [   {   'cpu-peak-load': 1},
                                                     {   'cpu-avg-load': 1}]},
                                 {   'memory-total': 1},
                                 {   'memory-used': 2}]},
            ]}
]

I would like to traverse this data structure and combine all the dict-element with the same name and create a list instead. 我想遍历这个数据结构并将所有dict-element与相同名称组合在一起,然后创建一个列表。 This is hard to explain and I have created an example structure of what I am looking for: 这很难解释,我创建了一个我正在寻找的示例结构:

{
    "ROOT": [
        {
            "firstElem": "gc-3/1/0", 
            "SecondElem": "5.0.0.1", 
            "ThirdElem": "128.0.2.19", 
            "index": "13", 
            "function": "session", 
            "hw": "1.11.0.0 ", 
            "sw": "1.50.0.228 ", 
            "resources": {
                "cpu-info": {
                    "cpu-peak-load": "1", 
                    "cpu-avg-load": "1",
                }, 
                "memory-total": "1", 
                "memory-used": "2", 
            }, 
        }, 
        {
            "firstElem": "gc-4/1/0", 
            "SecondElem": "5.0.0.1", 
            "ThirdElem": "128.0.2.19", 
            "index": "13", 
            "function": "session", 
            "hw": "1.11.0.0 ", 
            "sw": "1.50.0.228 ", 
            "resources": {
                "cpu-info": {
                    "cpu-peak-load": "8", 
                    "cpu-avg-load": "1", 
                }, 
                "memory-total": "1", 
                "memory-used": "2", 
            },  
        }
    ], 
}

I am stuck with the original data structure and can not change it . 我坚持使用原始数据结构, 无法更改它 Any help is appreciated. 任何帮助表示赞赏。 The structure provided above is just an example, since the data is received dynamically I will not know the tag-names. 上面提供的结构只是一个例子,因为数据是动态接收的,我不知道标签名称。 So please do not provide solutions that uses specific tag-name. 因此,请不要提供使用特定标记名称的解决方案。

let's try this: 让我们试试这个:

r = {}

def lst2dct(lst):
    return (lst if not isinstance(lst, list) else 
        {k: lst2dct(v) for e in lst for k, v in e.items()})

for e in source:
    key, val = e.items()[0]
    r.setdefault(key, []).append(lst2dct(val))

Here is a way: 这是一种方式:

>>> from collections import defaultdict
>>> def  combine(item):
    # Easy return if not a list: element itself
    if type(item) != type([]):
        return item
    # else call recursion
    first_ret = [(i.items()[0][0], combine(i.items()[0][1])) for i in item]

    # Here we group by same keys if any ('ROOT', for instance)
    count_keys = defaultdict(list)
    for couple in first_ret:
        count_keys[couple[0]].append(couple[1])
    return dict((k, v if len(v) > 1 else v[0]) for k, v in count_keys.iteritems())

I had to group the ROOT nodes, but it seems to be working: 我不得不对ROOT节点进行分组,但它似乎正在工作:

>>> pprint(combine(l))
{'ROOT': [{'SecondElem': '5.0.0.1',
           'ThirdElem': '127.3.15.1',
           'firstElem': 'gc-3/1/0',
           'function': 'session',
           'hw': '0.0.0.0',
           'index': 16,
           'resources': {'cpu-info': {'cpu-avg-load': 1,
                                      'cpu-peak-load': 1},
                         'memory-total': 1,
                         'memory-used': 2},
           'sw': '1.50.1.3'},
          {'SecondElem': '5.0.0.2',
           'ThirdElem': '127.3.4.1',
           'firstElem': 'gc-4/1/0',
           'function': 'stand',
           'hw': '0.0.0.0',
           'index': 5,
           'resources': {'cpu-info': {'cpu-avg-load': 1,
                                      'cpu-peak-load': 1},
                         'memory-total': 1,
                         'memory-used': 2},
           'sw': '1.50.1.3'}]}
>>> 

Its a bit hacky, but you could try: 它有点hacky,但你可以尝试:

data = [   {   'ROOT': [   
            {   'firstElem': 'gc-3/1/0'},
            {   'SecondElem': '5.0.0.1'},
            {   'ThirdElem': '127.3.15.1'},
            {   'index': 16},
            {   'function': 'session'},
            {   'hw': '0.0.0.0'},
            {   'sw': '1.50.1.3'},
            {   'resources': [   {   'cpu-info': [   {   'cpu-peak-load': 1},
                                                     {   'cpu-avg-load': 1}]},
                                 {   'memory-total': 1},
                                 {   'memory-used': 2}]},
            ]},
    {   'ROOT': [
            {   'firstElem': 'gc-4/1/0'},
            {   'SecondElem': '5.0.0.2'},
            {   'ThirdElem': '127.3.4.1'},
            {   'index': 5},
            {   'function': 'stand'},
            {   'hw': '0.0.0.0'},
            {   'sw': '1.50.1.3'},
            {   'resources': [   {   'cpu-info': [   {   'cpu-peak-load': 1},
                                                     {   'cpu-avg-load': 1}]},
                                 {   'memory-total': 1},
                                 {   'memory-used': 2}]},
            ]}
]

root_list = [

        ]

final_data = {
            'ROOT' : root_list
            }

for dict in data:
    if dict['ROOT'] not in final_data['ROOT']:
        final_data['ROOT'].append(dict['ROOT'])

I just did some here and there stuff in the interpreter and have come up with this: 我只是在解释器中做了一些这里和那里的东西,并提出了这个:

>>> i = [...] # your data
>>> rdict = {}
>>> for di in i:
 for root in di:
    if root not in rdict:
        rdict[root] = [di[root]]
    else:
        rdict[root] += [di[root]]

rdict is the type of dict you need, and it should work with multiple lists. rdict是你需要的dict类型,它应该适用于多个列表。


The function version of this: 这个功能版本:

def common_dict(list_of_dicts):
    i = list_of_dicts # less typing
    rdict = {}
    for di in i:
        for root in di:
            if root not in rdict:
                rdict[root] = [di[root]]
            else:
                rdict[root] += [di[root]]
return rdict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM