首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字典列表转换为唯一的字典列表

将字典列表转换为唯一的字典列表
EN

Stack Overflow用户
提问于 2013-02-12 16:32:41
回答 4查看 152关注 0票数 4

几个小时来,我一直在为解决以下问题而奋斗,但没有成功。

我有一个数据结构,如下所示:

代码语言:javascript
复制
[   {   '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}]},
            ]}
]

我想遍历这个数据结构,用相同的名称组合所有的dict元素,然后创建一个列表。这一点很难解释,我创建了一个示例结构来说明我想要的是什么:

代码语言:javascript
复制
{
    "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", 
            },  
        }
    ], 
}

我被原来的数据结构困住了,不能改变它,。任何帮助都是非常感谢的。上面提供的结构只是一个例子,因为数据是动态接收的,所以我将不知道标记名。因此,请不要提供使用特定标签名称的解决方案。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-12 17:41:01

以下是一种方法:

代码语言:javascript
复制
>>> 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())

我不得不对ROOT节点进行分组,但它似乎正在工作:

代码语言:javascript
复制
>>> 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'}]}
>>> 
票数 2
EN

Stack Overflow用户

发布于 2013-02-12 16:54:20

让我们试试这个:

代码语言:javascript
复制
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))
票数 2
EN

Stack Overflow用户

发布于 2013-02-12 16:58:36

有点烦人,但你可以试试:

代码语言:javascript
复制
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'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14837231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档