简体   繁体   中英

Create unique dictionaries for two keys in a list of dictionaries

I am getting a list of dictionaries in the following format from an API, :

eg.

xlist =[
    { "id":1, "day":2, name:"abc", ... },
    { "id":1, "day":3, name:"abc", ... },
    { "id":1, "day":2, name:"xyz", ... },
    { "id":1, "day":3, name:"xyz", ... },...
  ]   

So, to store/optimize the queries in to the DB I have to convert them in this format.

  • What is efficient or other way to generate following structure?

     unique_xlist =[ { "id":1, "day":2, name:["abc", "xyz"], ... }, { "id":1, "day":3, name:["abc", "xyz"], ... }, ] 

What I am doing :

 names = list(set([ v['name'] for v in xlist])) #-- GET UNIQUE NAMES
 templist =  [{k:(names if k == 'name' else v) \
           for k, v in obj.items()} for obj in xlist] #-- Append Unique names
 unique_xlist= {v['day']:v for v in templist}.values() #- find unique dicts

I don't think this is very efficient, am using 3 loops just to find unique dicts by day.

You could use itertools.groupby :

from itertools import groupby

xlist.sort(key=lambda x: (x["id"], x["day"], x["name"]))  # or use sorted()
unique_xlist = []

for k, g in groupby(xlist, lambda x: (x["id"], x["day"])):
    unique_xlist.append({"id": k[0], "day": k[1], "name": [i["name"] for i in g]})

Simply use the values that makes an item unique as keys to a dictionary:

grouped = {}
for x in xlist:
    key = (x['id'], x['day'])
    try:
        grouped[key]['name'].append(x['name'])
    except KeyError:
        grouped[key] = x
        grouped[key]['name'] = [x['name']]

You can listify this again afterwards if necessary.

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