在字典列表中使用相同的键组合两个相同的值

2024-04-29 12:24:15 发布

您现在位置:Python中文网/ 问答频道 /正文

完全是一个编程新手,需要一些帮助

我所拥有的是以下格式的词典列表:

a_list = [
    {'ID': 'a', 'Animal': 'dog', 'color': 'white', 'tail': 'yes'},
    {'ID': 'a', 'Animal': 'cat', 'color': 'black', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'bird', 'color': 'black', 'tail': 'no'},
    {'ID': 'b', 'Animal': 'cat', 'color': 'pink', 'tail': 'yes'}
    {'ID': 'b', 'Animal': 'dog', 'color': 'yellow', 'tail': 'no'}      
   ]

我想要的是一本词典,如下所示:

a_dict = 
    {'a': {'dog': {'color': 'white', 'tail': 'yes'},
           'cat': {'color': 'black', 'tail': 'yes'}},
     'b': {'bird': {'color': 'black', 'tail': 'no'},
           'cat': {'color': 'pink', 'tail': 'no'},
           'dog': {'color': 'yellow', 'tail': 'no'}}}    

Tags: noid编程cat词典yescolortail
3条回答
a_list = [
    {'ID': 'a', 'Animal': 'dog', 'color': 'white', 'tail': 'yes'},
    {'ID': 'a', 'Animal': 'cat', 'color': 'black', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'bird', 'color': 'black', 'tail': 'no'},
    {'ID': 'b', 'Animal': 'cat', 'color': 'pink', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'dog', 'color': 'yellow', 'tail': 'no'}      
]

a_dict = {}
for v in a_list:
    a_dict.setdefault(v['ID'], {}).setdefault(v['Animal'], {}).update(color=v['color'], tail=v['tail'])

from pprint import pprint
pprint(a_dict)

印刷品:

{'a': {'cat': {'color': 'black', 'tail': 'yes'},
       'dog': {'color': 'white', 'tail': 'yes'}},
 'b': {'bird': {'color': 'black', 'tail': 'no'},
       'cat': {'color': 'pink', 'tail': 'yes'},
       'dog': {'color': 'yellow', 'tail': 'no'}}}

使用defaultdict的简单解决方案

from collections import defaultdict
result = defaultdict(dict)
a_list = [
    {'ID': 'a', 'Animal': 'dog', 'color': 'white', 'tail': 'yes'},
    {'ID': 'a', 'Animal': 'cat', 'color': 'black', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'bird', 'color': 'black', 'tail': 'no'},
    {'ID': 'b', 'Animal': 'cat', 'color': 'pink', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'dog', 'color': 'yellow', 'tail': 'no'}      
   ]
for item in a_list:
    result[item['ID']][item['Animal']] = {'color':item['color'], 'tail':item['tail']}

defaultdict(dict,
            {'a': {'dog': {'color': 'white', 'tail': 'yes'},
              'cat': {'color': 'black', 'tail': 'yes'}},
             'b': {'bird': {'color': 'black', 'tail': 'no'},
              'cat': {'color': 'pink', 'tail': 'yes'},
              'dog': {'color': 'yellow', 'tail': 'no'}}})

创建了一个嵌套字典并删除了以后不需要的所有键

a_list = [
    {'ID': 'a', 'Animal': 'dog', 'color': 'white', 'tail': 'yes'},
    {'ID': 'a', 'Animal': 'cat', 'color': 'black', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'bird', 'color': 'black', 'tail': 'no'},
    {'ID': 'b', 'Animal': 'cat', 'color': 'pink', 'tail': 'yes'},
    {'ID': 'b', 'Animal': 'dog', 'color': 'yellow', 'tail': 'no'}      
   ]

a_dict = {}
for a in a_list:
    if a['ID'] in a_dict:
        a_dict[a['ID']][a['Animal']] = a
    else:
        a_dict[a['ID']] = {a['Animal']: a}

for id_ in a_dict:
    for animal in a_dict[id_]:
        del a_dict[id_][animal]['ID']
        del a_dict[id_][animal]['Animal']

输出:

>> a_dict

{'a': {'dog': {'color': 'white', 'tail': 'yes'},
  'cat': {'color': 'black', 'tail': 'yes'}},
 'b': {'bird': {'color': 'black', 'tail': 'no'},
  'cat': {'color': 'pink', 'tail': 'yes'},
  'dog': {'color': 'yellow', 'tail': 'no'}}}

相关问题 更多 >