Home » excel » excel – Intersect two dictionaries

excel – Intersect two dictionaries

Posted by: admin April 23, 2020 Leave a comment


Let’s say I have two dictionaries as following:

dict1 = {vessel1: [a, b, c], vessel2: [a, d, e], ...}

dict2 = {operation1: [a, d], operation2: [a, e, b], ...}

Each dictionary (dict1, dict2) is a dictionary of dictionaries, so that a, b, c, d, e, f and g are dictionaries too.

What I want is, for example, intersect dict1(vessel1) with dict2(operation2), and have a result dictionary as following:

result_dictionary_of_intersection = [a, b]

I.e., have a result dictionary that only contains items that both vessel1 and operation2 have.

Remembering: a and b are dictionaries too.

How to&Answers:

This will return the a and b dictionaries as you intended. This assumes that the key for the vessel1 dictionary is the string “vessel1”, and the key for the operation2 dictionary is “operation2”. You can of course, replace those string literals with variables in the code.

Dim newDict As Dictionary
Set newDict = New Dictionary

For Each myKey In dict1("vessel1").Keys

   If dict2("operation2").Exists(myKey) Then
        newDict.Add myKey, dict2("operation2")(myKey)
   End If


If you want a little more flexibility about what you are using for dict1 and dict2, you can do it this way (which actually makes the code a little more readable):

Set tmpDict1 = dict1("vessel1")
Set tmpDict2 = dict2("operation2")

Dim newDict As Dictionary
Set newDict = New Dictionary

For Each myKey In tmpDict1.Keys

   If tmpDict2.Exists(myKey) Then
        newDict.Add myKey, tmpDict2(myKey)
   End If



You may find this helpful:

Sub testMe()
Dim dict1 As New Dictionary
Dim dict2 As New Dictionary
Dim dict3 As New Dictionary

Dim dictAll As New Dictionary
Dim dictUnion As New Dictionary
Dim dictTemp As New Dictionary

dict1.Add "A", "A"
dict1.Add "B", "B"
dict1.Add "C", "C"
dict1.Add "D", "D"
dict1.Add "E", "E"

dict2.Add "C", "C"
dict2.Add "D", "D"
dict2.Add "E", "E"
dict2.Add "F", "F"

dict3.Add "C", "C"
dict3.Add "D", "D"

dictAll.Add 1, dict1
dictAll.Add 2, dict2
dictAll.Add 3, dict3

Dim var As Variant
Dim i As Integer

Set dictUnion = dictAll(1)

For Each var In dictAll
    Set dictTemp = dictAll(var)
    Set dictUnion = intMe(dictUnion, dictTemp)

'Set dictUnion = intMe(dict1, dict2)
For Each var In dictUnion
    Debug.Print var

Set dict1 = Nothing
Set dict2 = Nothing
Set dict3 = Nothing
Set dictAll = Nothing
Set dictUnion = Nothing
Set dictTemp = Nothing

End Sub

Function intMe(dict1 As Dictionary, dict2 As Dictionary) As Dictionary
Dim dictInt As New Dictionary
Dim var As Variant
For Each var In dict1.Keys
    If dict2.Exists(var) Then
        dictInt.Add var, var
    End If
Set intMe = dictInt
End Function

Sub Intersect(dAll As Dictionary)

Dim var As Variant
Dim subVar As Variant
Dim dict As Dictionary

For Each var In dAll

    If var <> "Account_LV0" And var <> "Account_LV1" Then

        Set dict = dAll(var)
        Debug.Print var & "|" & dict.Count
        'For Each subVar In dict.Keys
        '    Debug.Print subVar
    End If

End Sub