Intersection of Dictionary based on keys and values

Sabz

I have a couple of dictionaries that I want to perform intersection on. two dictionaries might have different lists of values for the same key.

Example:

Dictionary<int, List<int>> Primary = new Dictionary<int, List<int>>();
Primary.Add(1, new List<int>());
Primary[1].Add(5);
Primary[1].Add(6);
Primary[1].Add(7);

Primary.Add(2, new List<int>());
Primary[2].Add(5);

Dictionary<int, List<int>> Secondary = new Dictionary<int, List<int>>();
Secondary.Add(1, new List<int>());
Secondary[1].Add(6);
Secondary[1].Add(7);
Secondary[1].Add(8);

Secondary.Add(3, new List<int>());
Secondary[3].Add(5);

So I want the resultant dictionary to contain only:

[1],[list<6,7>]

How can I achieve this? The actual data structure where I want the intersection is of the form:

Dictionary<long, SortedList<int,List<long>>>
BartoszKP

You can achieve this as follows:

var result = Primary.Keys.Intersect(Secondary.Keys)
                 .ToDictionary(key => key,
                               key => Primary[key].Intersect(Secondary[key]).ToList());

Transforming the result into Dictionary<long, SortedList<int,List<long>>> should be easy enough.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find intersection of dictionary values list

How to filter dictionary keys based on its corresponding values

How to filter dictionary keys based on its corresponding values

Intersection of lists which are values in a dictionary

Python: how to slice a dictionary based on the values of its keys?

Loop through dictionary keys to get values based on input

Add dictionary values for common keys based on categorical variables in another column

Selecting Column values based on dictionary keys

How to group dictionary keys based on their list values matching condition?

Create array of values based on dictionary and array of keys

intersection across multiple dictionary keys

Form a python dictionary based on pattern matched keys and values in file

Build a new dictionary with concatenated duplicated keys based on values

Modify values in a nested dictionary based on their keys

Sorting the keys of a dictionary based on different values

Changing dictionary values based on matches between dictionary keys and a list

create dictionary of values based on matching keys in list from nested dictionary

combining dictionary keys based on values and some conditions

Sum values in a dictionary based on condition that matches on keys in Python

How to update dictionary values based on values from another dictionary having same keys where values are list?

How to loop through a dictionary and assign values to a variable based on keys?

Can I absorb the values of dictionary based on their keys?

Intersection of values based on union of keys

Replace Pandas DataFrame column values based on containing dictionary keys

change each keys of a dictionary based on length of each values

How to transform dictionary keys into a dataframe column based on the values if the values are lists

Finding the length of keys of dictionary based on values

Replacing the keys in a dictionary based on the values of another

Assign dictionary values to specific columns based on dictionary keys