简体   繁体   English

从Python中的字典列表创建列表字典

[英]Create Dictionary Of Lists from List of Dictionaries in Python

I have a list of dictionaries as follows. 我有以下词典列表。

[{'a' : 1, 'b' : 2, 'c' : 2},
 {'a' : 2, 'b' : 3, 'c' : 3},
 {'a' : 3, 'b' : 5, 'c' : 6},
 {'a' : 4, 'b' : 7, 'c' : 8},
 {'a' : 1, 'b' : 8, 'c' : 9},
 {'a' : 2, 'b' : 0, 'c' : 0},
 {'a' : 5, 'b' : 1, 'c' : 3},
 {'a' : 7, 'b' : 4, 'c' : 5}]

I want to create a dictionary of lists from above list which should be as follows. 我想从上面的列表创建一个列表字典,如下所示。

{1 : [{'a' : 1, 'b' : 2, 'c' : 2}, {'a' : 1, 'b' : 8, 'c' : 9}]
 2 : [{'a' : 2, 'b' : 3, 'c' : 3}, {'a' : 2, 'b' : 0, 'c' : 0}]
 3 : [{'a' : 3, 'b' : 5, 'c' : 6}]
 4 : [{'a' : 4, 'b' : 7, 'c' : 8}]
 5 : [{'a' : 5, 'b' : 1, 'c' : 3}]
 7 : [{'a' : 7, 'b' : 4, 'c' : 5}]

Basically I want to pick one of the keys in dictionary say 'a', and create new dictionary with the values of that key (1, 2, 3, 4, 5, 7) as keys for new dictionary to be created, and values for new dictionary should be list of all the dictionaries containing that value as value for key 'a'. 基本上,我想选择字典中的一个键说'a',然后用该键的值(1、2、3、4、5、7)创建新的字典,作为要创建的新字典的键和值对于新字典,应该是包含该值作为键“ a”的值的所有词典的列表。

I know the simplest approach is iterating over the list and build the required dictionary. 我知道最简单的方法是遍历列表并构建所需的字典。 I am just curious is there another way of doing it. 我很好奇,还有另一种方法可以做到这一点。

A collections.defaultdict will be the most efficient: 一个collections.defaultdict将是最有效的:

from collections import defaultdict

l = [{'a': 1, 'b': 2, 'c': 2},
 {'a': 2, 'b': 3, 'c': 3},
 {'a': 3, 'b': 5, 'c': 6},
 {'a': 4, 'b': 7, 'c': 8},
 {'a': 1, 'b': 8, 'c': 9},
 {'a': 2, 'b': 0, 'c': 0},
 {'a': 5, 'b': 1, 'c': 3},
 {'a': 7, 'b': 4, 'c': 5}]

dct = defaultdict(list)

for d in l:
    dct[d["a"]].append(d)

from pprint import pprint as pp
pp(dict(dct))

Output: 输出:

{1: [{'a': 1, 'b': 2, 'c': 2}, {'a': 1, 'b': 8, 'c': 9}],
 2: [{'a': 2, 'b': 3, 'c': 3}, {'a': 2, 'b': 0, 'c': 0}],
 3: [{'a': 3, 'b': 5, 'c': 6}],
 4: [{'a': 4, 'b': 7, 'c': 8}],
 5: [{'a': 5, 'b': 1, 'c': 3}],
 7: [{'a': 7, 'b': 4, 'c': 5}]}

Normal dictionary with setdefault method can be used 可以使用带有setdefault方法的普通字典

Code: 码:

data=[{'a' : 1, 'b' : 2, 'c' : 2},
{'a' : 2, 'b' : 3, 'c' : 3},
{'a' : 3, 'b' : 5, 'c' : 6},
{'a' : 4, 'b' : 7, 'c' : 8},
{'a' : 1, 'b' : 8, 'c' : 9},
{'a' : 2, 'b' : 0, 'c' : 0},
{'a' : 5, 'b' : 1, 'c' : 3},
{'a' : 7, 'b' : 4, 'c' : 5}]

dictionary_list={}
for row in data:
    dictionary_list.setdefault(row["a"],[]).append(row)
print dictionary_list

Output: 输出:

{1: [{'a': 1, 'c': 2, 'b': 2}, {'a': 1, 'c': 9, 'b': 8}],
2: [{'a': 2, 'c': 3, 'b': 3}, {'a': 2, 'c': 0, 'b': 0}],
3: [{'a': 3, 'c': 6, 'b': 5}],
4: [{'a': 4, 'c': 8, 'b': 7}],
5: [{'a': 5, 'c': 3, 'b': 1}],
7: [{'a': 7, 'c': 5, 'b': 4}]}

You can do it in following way 您可以按照以下方式进行

mylist = [
   {'a' : 1, 'b' : 2, 'c' : 2},
   {'a' : 2, 'b' : 3, 'c' : 3},
   {'a' : 3, 'b' : 5, 'c' : 6},
   {'a' : 4, 'b' : 7, 'c' : 8},
   {'a' : 1, 'b' : 8, 'c' : 9},
   {'a' : 2, 'b' : 0, 'c' : 0},
   {'a' : 5, 'b' : 1, 'c' : 3},
   {'a' : 7, 'b' : 4, 'c' : 5}
]

def get_dict(mylist, required_key):
    result_dict = {}

    for mydict in mylist:
        result_dict.setdefault(mydict[required_key], [])
        result_dict[mydict[required_key]].append(mydict)

    return result_dict

result_dict = get_dict(mylist, required_key = 'a')
print(result_dict)

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

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