简体   繁体   中英

How to combine the list of dictionaries using values of the key?

Inputs:

      values = [{'priority': 'P2', 'subdomain': 'SD2', 'test': '2653'},
               {'priority': 'P3', 'subdomain': 'SD1', 'test': '2656'},
               {'priority': 'P2', 'subdomain': 'SD2', 'test': '2651'},
               {'priority': 'P2', 'subdomain': 'SD2', 'test': '2652'},
               {'priority': 'P3', 'subdomain': 'SD1', 'test': '2655'}]

should be converted to:

      values = [{'priority': 'P2', 'subdomain': 'SD2', 'test': '2653,2651,2652'},
                {'priority': 'P3', 'subdomain': 'SD1', 'test': '2656,2655'}]

The value for key is not fixed it will change based on user needs.

I tried:

result_dict["test"] = values[0]["test"]
final_output = []

I = 1
for value in range(len(values)):

    print("value", value)


    for j in range(I, len(values)):
           print("j", j)

           I = I+1
           if values[value]["priority"] == values[j]["priority"] \
                and values[value]["subdomain"] == values[j]["subdomain"]:
              final_output = []
              result_dict["priority"] = values[value]["priority"]
              result_dict["subdomain"] = values[value]["subdomain"]
              result_dict["test"] = result_dict["test"]+","+values[j]["test"]
              count = count +1

             
              
              final_output.append(result_dict)
              print(final_output)

Please help me out of this.

loop over the list and build a new dictionary:

def combine_dict_rows(values):

    result = {}
    for row in values:
        if row['priority'] not in result:
            result[row['priority']] = {}
        if row['subdomain'] not in result[row['priority']]:
            result[row['priority']][row['subdomain']] = []
        result[row['priority']][row['subdomain']].append(row['test'])
    return result

values = [{'priority': 'P2', 'subdomain': 'SD2', 'test': '2653'},
               {'priority': 'P3', 'subdomain': 'SD1', 'test': '2656'},
               {'priority': 'P2', 'subdomain': 'SD2', 'test': '2651'},
               {'priority': 'P2', 'subdomain': 'SD2', 'test': '2652'},
               {'priority': 'P3', 'subdomain': 'SD1', 'test': '2655'}]

 print(combine_dict_rows(values))

output:

{'P2': {'SD2': ['2653', '2651', '2652']}, 'P3': {'SD1': ['2656', '2655']}}

You can use groupby and sort with the same key:

from itertools import groupby 

def key_func(d, keys=['priority', 'subdomain']):
    return [d.get(key, "") for key in keys]

new_li=[]   
for k,v in groupby(sorted(values, key=key_func), key=key_func):
    vals=list(v)
    di=vals[0]
    di['test']=','.join(d['test'] for d in vals)
    # di['test']=[d['test'] for d in vals] if you want a list
    new_li.append(di)

>>> new_li
[{'priority': 'P2', 'subdomain': 'SD2', 'test': '2653,2651,2652'}, {'priority': 'P3', 'subdomain': 'SD1', 'test': '2656,2655'}]

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