Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...