Пересечь два словаря - PullRequest
3 голосов
/ 23 марта 2011

Допустим, у меня есть следующие два словаря:

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

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

Каждый словарь (dict1, dict2) является словарем словарей, так что a, b, c, d, e, f и g словари тоже.

Я хочу, например, пересечь dict1(vessel1) с dict2(operation2) и получить следующий словарь результатов:

result_dictionary_of_intersection = [a, b]

Т.е., иметь словарь результатов, который содержит только те элементы, которые есть и в случае судна1, и в операции 2.

Помня: a и b тоже словари.

Ответы [ 2 ]

2 голосов
/ 23 марта 2011

Это вернет словари a и b, как вы и предполагали. Это предполагает, что ключом для словаря vessel1 является строка «vessel1», а ключом для словаря operation2 является «operation2». Конечно, вы можете заменить эти строковые литералы переменными в коде.

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

Next

Если вы хотите немного больше гибкости в том, что вы используете для dict1 и dict2, вы можете сделать это следующим образом (что фактически делает код немного более читабельным):

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

Next
0 голосов
/ 25 марта 2013

Вы можете найти это полезным:

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)
Next

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

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
Next
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
        'Next
    End If
Next


End Sub
...