简体   繁体   English

从字典列表创建新字典

[英]Creating a new dictionary from a list of dictionaries

I have a list of dictionaries, that looks like this:我有一个字典列表,如下所示:

my_dicts = 
[{'1A': 1, '3E': 2, 'PRODUCT NAME': 'White Bread loaf large', 'Week': 1},
 {'1A': 1, '1B': 1, '1C': 1, '1D': 2, '1E': 2, '2C': 1, '3E': 2, 'PRODUCT NAME': 'Brown Bread loaf 
 large', 'Week': 1}...]

I want to create a new dictionary, that looks like this:我想创建一个新字典,如下所示:

new_dict = 
[{'HOUSE NAME': '1A', 'White Bread Loaf Large' : 1, 'Brown Bread loaf large' : 1},
 {'HOUSE NAME': '1B', 'Brown Bread loaf large' : 1},...
 {'HOUSE NAME': '3E', 'White Bread Loaf Large' : 2, 'Brown Bread Loaf Large' : 2}]

Each 'HOUSE NAME' is unique.每个“房屋名称”都是独一无二的。

I have a solution, that creates the new dictionary, and works with the sample list I've provided, but it does not work for my actual list (that contains 27 dictionaries)我有一个解决方案,可以创建新字典,并使用我提供的示例列表,但它不适用于我的实际列表(包含 27 个字典)

This is the solution:这是解决方案:

houses = set(['1A', '1B', '1C', '1D', '3E'])

output_list = []

for house in houses:
    output_entry = {}
    output_entry["HOUSE NAME"] = house
    for entry in my_dicts:
        if entry.get(house) and entry.get("PRODUCT NAME"):
            product_name = entry.get("PRODUCT NAME")
            if output_entry.get(product_name):
                output_entry[product_name] += 1
            else:
                output_entry[product_name] = 1

    output_list.append(output_entry)

The solution's last if statement doesnt seem to work, as whatever I set the 'else's condition to, is what my values are set to.解决方案的最后一个 if 语句似乎不起作用,因为无论我将 'else 的条件设置为什么,我的值设置为什么。

You could do it like this:你可以这样做:

my_dicts = [{'1A': 1, '3E': 2, 
             'PRODUCT NAME': 'White Bread loaf large', 'Week': 1},
            {'1A': 1, '1B': 1, '1C': 1, '1D': 2, '1E': 2, '2C': 1, '3E': 2,
             'PRODUCT NAME': 'Brown Bread loaf large', 'Week': 1}]

merged = dict()
for d in my_dicts:
    for k,v in d.items():
        if k in ('PRODUCT NAME','Week'): continue # only process house names
        merged.setdefault(k,{'HOUSE NAME':k}).update({d['PRODUCT NAME']:v})
result = list(merged.values())

print(result)

[{'HOUSE NAME': '1A', 'White Bread loaf large': 1, 'Brown Bread loaf large': 1},
 {'HOUSE NAME': '3E', 'White Bread loaf large': 2, 'Brown Bread loaf large': 2},
 {'HOUSE NAME': '1B', 'Brown Bread loaf large': 1},
 {'HOUSE NAME': '1C', 'Brown Bread loaf large': 1},
 {'HOUSE NAME': '1D', 'Brown Bread loaf large': 2},
 {'HOUSE NAME': '1E', 'Brown Bread loaf large': 2},
 {'HOUSE NAME': '2C', 'Brown Bread loaf large': 1}]

The merged dictionary serves as a temporary index to assemble the new list of dictionaries by updating them based on each house name. merged的字典用作临时索引,通过根据每个房屋名称更新字典来组装新的字典列表。 Then the final result is obtained by ignoring the indexing par of merged , only taking the values (ie dictionaries merged by house)然后通过忽略merged的索引par得到最终结果,只取值(即按house合并的字典)

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

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