简体   繁体   中英

Create a new list of dictionaries from a main list and a sublist

I have two lists (the main list and the sublist) containing different numbers of dictionaries and their values would be the same if their positions match.

mainLst = [{"name":"M_AAA_X", "position":"1", "value":"8"}, 
           {"name":"M_AAA_X", "position":"2", "value":"10"}, 
           {"name":"M_AAA_X", "position":"4", "value":"14"}, 
           {"name":"M_AAA_X", "position":"5", "value":"16"},
           {"name":"M_AAA_X", "position":"7", "value":"20"}]
subLst = [{"name":"S_AAA_X", "position":"1", "value":"8"}, 
          {"name":"S_AAA_X", "position":"2", "value":"10"}, 
          {"name":"S_AAA_X", "position":"3", "value":"12"}, 
          {"name":"S_AAA_X", "position":"4", "value":"14"}, 
          {"name":"S_AAA_X", "position":"5", "value":"16"},
          {"name":"S_AAA_X", "position":"6", "value":"18"}]

I would like to create a new list by:

  1. taking elements from the mainLst
  2. if the mainLst lacks a certain position (ex. position: 3, 6), take elements from the subList
  3. sort by 'position' in ascending order

The output should look like this:

newLst = [{"name":"M_AAA_X", "position":"1", "value":"8"}, 
          {"name":"M_AAA_X", "position":"2", "value":"10"}, 
          {"name":"S_AAA_X", "position":"3", "value":"12"},
          {"name":"M_AAA_X", "position":"4", "value":"14"}, 
          {"name":"M_AAA_X", "position":"5", "value":"16"},
          {"name":"S_AAA_X", "position":"6", "value":"18"},
          {"name":"M_AAA_X", "position":"7", "value":"20"}]

Could anyone help me to accomplish this? Thanks in advance!

Give this a try.

mainLst.extend([dictSub for dictSub in subLst if dictSub['position'] not in [pos['position'] for pos in mainLst]])
print(sorted(mainLst, key = lambda i: i['position']))

Basically we gather positions in sublist which is not in mainlist . Then we sort the list of dicts based on that.

output

[{'name': 'M_AAA_X', 'position': '1', 'value': '8'}, 
 {'name': 'M_AAA_X', 'position': '2', 'value': '10'}, 
 {'name': 'S_AAA_X', 'position': '3', 'value': '12'}, 
 {'name': 'M_AAA_X', 'position': '4', 'value': '14'}, 
 {'name': 'M_AAA_X', 'position': '5', 'value': '16'}, 
 {'name': 'S_AAA_X', 'position': '6', 'value': '18'}, 
 {'name': 'M_AAA_X', 'position': '7', 'value': '20'}]

Another solution, using itertools.groupby :

from itertools import groupby

out = []
for _, g in groupby(
    sorted(mainLst + subLst, key=lambda k: int(k["position"])),
    lambda k: int(k["position"]),
):
    out.append(next(g))

print(out)

Prints:

[{'name': 'M_AAA_X', 'position': '1', 'value': '8'}, 
 {'name': 'M_AAA_X', 'position': '2', 'value': '10'}, 
 {'name': 'S_AAA_X', 'position': '3', 'value': '12'}, 
 {'name': 'M_AAA_X', 'position': '4', 'value': '14'}, 
 {'name': 'M_AAA_X', 'position': '5', 'value': '16'}, 
 {'name': 'S_AAA_X', 'position': '6', 'value': '18'}, 
 {'name': 'M_AAA_X', 'position': '7', 'value': '20'}]

I have two lists (the main list and the sublist) containing different numbers of dictionaries and their values would be the same if their positions match.

mainLst = [{"name":"M_AAA_X", "position":"1", "value":"8"}, 
           {"name":"M_AAA_X", "position":"2", "value":"10"}, 
           {"name":"M_AAA_X", "position":"4", "value":"14"}, 
           {"name":"M_AAA_X", "position":"5", "value":"16"},
           {"name":"M_AAA_X", "position":"7", "value":"20"}]
subLst = [{"name":"S_AAA_X", "position":"1", "value":"8"}, 
          {"name":"S_AAA_X", "position":"2", "value":"10"}, 
          {"name":"S_AAA_X", "position":"3", "value":"12"}, 
          {"name":"S_AAA_X", "position":"4", "value":"14"}, 
          {"name":"S_AAA_X", "position":"5", "value":"16"},
          {"name":"S_AAA_X", "position":"6", "value":"18"}]

I would like to create a new list by:

  1. taking elements from the mainLst
  2. if the mainLst lacks a certain position (ex. position: 3, 6), take elements from the subList
  3. sort by 'position' in ascending order

The output should look like this:

newLst = [{"name":"M_AAA_X", "position":"1", "value":"8"}, 
          {"name":"M_AAA_X", "position":"2", "value":"10"}, 
          {"name":"S_AAA_X", "position":"3", "value":"12"},
          {"name":"M_AAA_X", "position":"4", "value":"14"}, 
          {"name":"M_AAA_X", "position":"5", "value":"16"},
          {"name":"S_AAA_X", "position":"6", "value":"18"},
          {"name":"M_AAA_X", "position":"7", "value":"20"}]

Could anyone help me to accomplish this? Thanks in advance!

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