简体   繁体   中英

combine two python lists of dict with index

I want to merge two lists into a nested construct.

List_A: [{'id':1,'val':'abc'},{'id':2,'val':'bcd'},{'id':3,'val':'efg'}]
List_B: [{'ref':1,'name':'xyz'},{'ref':2,'name':'opq'},{'ref:2,'name':'jkl'},{'ref':3,'name':'nmo'}]

Result should be:

List_C: [{'id':1,'list_b':[{'name':'xyz'}]},{'id':2,'list_b':[{'name':'opq'},{'name':'jkl'}]},{'id':3,'list_b':[{'name':'nmo'}]}]

I tried pandas join and merge but got no result. Sure, I could iterate through the list and do it key by key, but there might be a much better solution.

Based on your desired output, you don't need List_A at all.

from collections import defaultdict

b = [{'ref':1,'name':'xyz'}, {'ref':2,'name':'opq'}, {'ref':2,'name':'jkl'},{'ref':3,'name':'nmo'}]
dct = {}
[dct.setdefault(tuple(i), list()).append(j) for *i, j in [tuple(x.values()) for x in b]]
[{'id':k[0], 'list_b':list({'name': n} for n in v)} for k, v in dct.items()]

Output:

[{'id': 1, 'list_b': [{'name': 'xyz'}]},
 {'id': 2, 'list_b': [{'name': 'opq'}, {'name': 'jkl'}]},
 {'id': 3, 'list_b': [{'name': 'nmo'}]}]

Overall this seems like an over-complicated way to store the data, but if you need that specific format (ex for an input to another program) then w/e.

from copy import deepcopy
List_C = deepcopy(List_A)
for dct1 in List_C:
    dct1.pop('val')
    dct1['list_b'] = list()
    for dct2 in List_B:
        if dct1['id'] == dct2['ref']:
            dct3 = dct2.copy()
            dct3.pop('ref')
            dct1['list_b'].append(dct3)

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