Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

python - 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)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. Then the final result is obtained by ignoring the indexing par of merged, only taking the values (i.e. dictionaries merged by house)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...