简体   繁体   中英

Merge Lists in Dictionary and remove duplicates

I only found questions where people wanted to merge lists into dictionaries or merge dictionaries but not merge lists that are already in a dictionary

Lets say I have a Dictionary having following structure

myDict= {
  'key1': [{'description': 'some description', 'listwithstrings': ['somestring1'], 'number': '1'}, {'listwithstrings': ['somestring1', 'somestring2'], 'description': 'some other description', 'number': '1'}],
  'key2': [{'listwithstrings': ['somestring4'], 'description': "some different description, 'number': '2'}, {'number': '2', 'listwithstrings': ['somestring5'], 'description': 'some different description'}],
  'key3': [{'number': '3', 'listwithstrings': ['somestring7', 'somestring8'], 'description': 'only one entry'}]
  }

now I want to merge the entries in the dictionary from each key for itself and remove the duplicates. I don't know for each key whether it has multiple entries (it can have more than two, too) or just one, so I can't use the key as a condition like number==1

Resulting in

myCleanedDict= {
  'key1': [{'description': ['some description', 'some other description'], 'listwithstrings': ['somestring1', 'somestring2'], 'number': '1'}],
  'key2': [{'listwithstrings': ['somestring4', 'somestring5'], 'description': 'some different description', 'number': '2'}],
  'key3': [{'number': '3', 'listwithstrings': ['somestring7', 'somestring8'], 'description': 'only one entry'}]
  }
myDict = {
    'key1': [
        {
            'description': 'some description', 
            'listwithstrings': ['somestring1'], 
            'number': '1'
        }, 
        {
            'listwithstrings': ['somestring1', 'somestring2'], 
            'description': 'some other description', 
            'number': '1'
        }
    ],
    'key2': [
        {
            'listwithstrings': ['somestring4'], 
            'description': 'some different description', 
            'number': '2'
        }, 
        {
            'number': '2', 
            'listwithstrings': ['somestring5'], 
            'description': 'some different description'
        }
    ],
    'key3': [
        {
            'number': '3', 
            'listwithstrings': ['somestring7', 'somestring8'], 
            'description': 'only one entry'
        }
    ]
}

newDict = {}
for k, v in myDict.items():
    if len(v) == 0: continue
    target = v[0]
    for k in target:
        if not isinstance(target[k], list):
            target[k] = [target[k]]
    for i in range(1, len(v)):
        for k, v in v[i].items():
            if isinstance(v, list):
                target[k] += v
            else:
                target[k].append(v)
            target[k] = list(set(target[k]))
    for k in target:
        if len(target[k]) == 1:
            target[k] = target[k][0]
    newDict[k] = [target]

print(newDict)

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