0

I tried to google and find a problem which is very similar to my use case here: combine dictionaries in list of dictionaries based on matching key:value pair. But it seems that it did not correspond 100% to my case as I have list of nested dictionaries. Suppose that I have a list of nested dictionaries (more than 2) but in this case I considered two nested dictionaries to make the example:

my_list = [{'sentence': ['x',
   'ray',
   'diffractometry',
   'has',
   'been',
   'largely',
   'used',
   'thanks',
   'to',
   ],
  'mentions': [{'mention': [27, 28],
    'positives': [26278, 27735, 21063],
    'negatives': [],
    'entity': 27735}]},
 {'sentence': ['x',
   'ray',
   'diffractometry',
   'has',
   'been',
   'largely',
   'used',
   'thanks',
   'to',
   ],
  'mentions': [{'mention': [13, 14],
    'positives': [7654],
    'negatives': [],
    'entity': 7654}]}]

How can I merge these two dictionaries based on the matching of key(sentence) and value(list of all tokens) So that I can get the desired result as below:

my_new_list = [
{'sentence': ['x',
   'ray',
   'diffractometry',
   'has',
   'been',
   'largely',
   'used',
   'thanks',
   'to',
   ],
  'mentions': [
    {'mention': [27, 28],
    'positives': [26278, 27735, 21063],
    'negatives': [],
    'entity': 27735
    },
   {'mention': [13, 14],
    'positives': [7654],
    'negatives': [],
    'entity': 7654
     }
   ]
}
]

How to merge the list of key "mentions" when matching the key(sentence):value(list of all tokens)? In my actual list, there will be a lot of dictionaries with the same style.

Many thanks for your help.

2
  • 1
    You should make your example smaller. That will help you and us :) Commented Aug 18, 2021 at 9:41
  • 1
    @log0-- Thank for the advice. I reduce some tokens from the key "sentence". Commented Aug 18, 2021 at 9:44

2 Answers 2

1
my_dict = {}
for row in my_list:
    key = ' '.join(row['sentence']) # use sentence as key
    if key in my_dict:
        my_dict[key]['mentions'].extend(row['mentions'])
    else:
        my_dict[key] = row
        
my_list = list(my_dict.values())
Sign up to request clarification or add additional context in comments.

1 Comment

What if the keys are dynamic? lets say key 'mentions' can be a different name based on the list of dicts passed by the user
0

From what I understand you want to group information by "sentence".

You can do this by iterating on your array and fill a dictionary of list indexed by sentence.

Something like:

from collections import defaultdict
sentences = defaultdict(list)
for element in my_list:
   key = tuple(element["sentence"])
   sentences[key].append(element)

this gives you

 { sentence1: [element1, element2], sentence2: [element3] }

From there should be able to easily construct the structure you want.

edit removed reference to specific fields

6 Comments

Can you purpose a solution with the codes? As I tried with the linked I search but i could not solve the problem.
Converted the sentence to a tuple to make it hashable (usable as a key in a dictionary)
The solution did not work when you have more than two lists in it.
@Erwinwin What do you mean ? can you write down the structure
I mean this solution did not work when you have more than two nested dictionaries in that list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.