简体   繁体   中英

Transform dictionary python

I have a list of dictionaries in this form:

d_old=[{"color":"red","store":"1","s_d":{"M":"1","L":"2"}},       
       {"color":"blue","store":"2","s_d":{"S":"3","XL":"4"}}]

My goal is to transform this into:

d_new=[{"color":"red","store":"1","Size":"M", "Stock":"1"},
       {"color":"red","store":"1","Size":"L", "Stock":"2"},
       {"color":"blue","store":"2","Size":"S", "Stock":"3"},
       {"color":"blue","store":"2","Size":"XL", "Stock":"4"}]

I wrote this, it works but I like to know if there is a better way: The keys are always defined in a list, and here for simplification, only 2 keys are shown, there is more.

def transform_data(d_old):
    d_new_list =[]
    headers=["color","store"]
    for d in d_old:
        for key,value in d["s_d"].items():
            temp_dict = {header:d[header] for header in headers}
            temp_dict["Size"] = key
            temp_dict["Stock"] = value
            d_new_list.append(temp_dict)

you could use a nested list comprehension :

d_old = [{'color': 'red', 'store': '1', 's_d': {'M': '1', 'L': '2'}},
         {'color': 'blue', 'store': '2', 's_d': {'S': '3', 'XL': '4'}}]

d_new = [{'color': item['color'], 'store': item['store'],
          'size': size, 'stock': stock}
         for item in d_old for size, stock in item['s_d'].items()]

if you do not want to hardcode the keys:

d_new = [dict({key: values for key, values in item.items() if key != 's_d'},
              size=size, stock=stock)
         for item in d_old for size, stock in item['s_d'].items()]

If you don't want hardcode the keys, you can try this Pythonic way

>>> [dict({"Size":k,"Stock":v},**i) for i in d_old for k,v in i.pop("s_d").items()]

[{'color': 'red', 'Size': 'M', 'store': '1', 'Stock': '1'}, 
 {'color': 'red', 'Size': 'L', 'store': '1', 'Stock': '2'}, 
 {'color': 'blue', 'Size': 'S', 'store': '2', 'Stock': '3'}, 
 {'color': 'blue', 'Size': 'XL', 'store': '2', 'Stock': '4'}]

dict({"Size":k,"Stock":v},**i) can merge the new dict with the old dict, after you removed the s_d by dict.pop() method.

By the way, if you want keep the d_old :

Try this way:

>>> [dict({"Size":k,"Stock":v},**{m:i[m] for m in i if m!='s_d'}) for i in d_old for k,v in i["s_d"].items()]
[{'color': 'red', 'Size': 'M', 'store': '1', 'Stock': '1'}, {'color': 'red', 'Size': 'L', 'store': '1', 'Stock': '2'}, {'color': 'blue', 'Size': 'S', 'store': '2', 'Stock': '3'}, {'color': 'blue', 'Size': 'XL', 'store': '2', 'Stock': '4'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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