Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

python - How to merge 2 lists of dictionaries so keys with None values will be always replaced (if not None values exist)

So I have 2 lists of dictionaries I would like to merge:

posix_groups=[dict(name='group_A', gid=8888881, sid=None),
              dict(name='group_B', gid=8888882, sid=None),
              dict(name='group_C', gid=8888883, sid=None)]

windows_groups=[dict(name='group_A', gid=8888881, sid='S-1-5-21-927172180-3694312366-24219317-65317'),
                dict(name='group_B', gid=None, sid='S-1-5-21-927172180-3694312366-24219317-65318'),
                dict(name='group_C', gid=None, sid='S-1-5-21-927172180-3694312366-24219317-65319'),
                dict(name='group_Z', gid=6666666, sid='S-1-5-21-927172180-3694312366-24219317-76207')]

Desired output is as follows:

merged_groups=[dict(name='group_A', gid=8888881, sid='S-1-5-21-927172180-3694312366-24219317-65317'),
               dict(name='group_B', gid=8888882, sid='S-1-5-21-927172180-3694312366-24219317-65318'),
               dict(name='group_C', gid=8888883, sid='S-1-5-21-927172180-3694312366-24219317-65319'),
               dict(name='group_Z', gid=6666666, sid='S-1-5-21-927172180-3694312366-24219317-76207')]

Is there elegant (pythonic) way to do it?

Thanks!

question from:https://stackoverflow.com/questions/65882602/how-to-merge-2-lists-of-dictionaries-so-keys-with-none-values-will-be-always-rep

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could go along the following lines:

# map by name
pg = {d["name"]: d for d in posix_groups}
wg = {d["name"]: d for d in windows_groups}
names = pg.keys() | wg.keys()  # all names in either list

# readability helpers
val = lambda d, n, k: d.get(n, {}).get(k)
keys = lambda n: pg.get(n, {}).keys() | wg.get(n, {}).keys()

merged_groups = [
    {k: val(pg, n, k) or val(wg, n, k) for k in keys(n)} 
    for n in names
]

[{'gid': 6666666,
  'name': 'group_Z',
  'sid': 'S-1-5-21-927172180-3694312366-24219317-76207'},
 {'gid': 8888882,
  'name': 'group_B',
  'sid': 'S-1-5-21-927172180-3694312366-24219317-65318'},
 {'gid': 8888883,
  'name': 'group_C',
  'sid': 'S-1-5-21-927172180-3694312366-24219317-65319'},
 {'gid': 8888881,
  'name': 'group_A',
  'sid': 'S-1-5-21-927172180-3694312366-24219317-65317'}]

This uses the union of dictionary keys via | and the short-circuit behaviour of the or operator that evalutes to the first truthy operand.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...