2011-03-23 5 views
3

의 나는 다음과 같은 두 개의 사전을 가지고 있다고 가정 해 봅시다 :교차 두 개의 사전

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

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

각 사전 (dict1, dict2) 사전의 사전입니다 a, b, c, d, e, fg 그래서 사전도 있습니다.

은 내가 원하는, 예를 들어, dict2(operation2)dict1(vessel1) 교차하고, 다음과 같은 결과 사전이 있습니다

result_dictionary_of_intersection = [a, b] 

즉, 단지 vessel1 및 operation2 모두가이 항목을 포함하는 결과 사전을 가지고있다.

기억 : ab도 사전입니다.

+1

제목에 태그를 넣을 필요가 없습니다. – Will

+0

교차하는 사전이 의미하는 바가 내게 명확하지 않습니다. 사전과 같은 사전을 사용하려면 http://stackoverflow.com/questions/4749698/excel-vba-is-there-anything-like-javas-set-container-in-vba/4751101#4751101을 참조하십시오. . [a, b, c]와 [a, e, b]가리스트 (또는 콜렉션, 배열 등)를 나타내고, 각리스트의 a, b 등이 동일한 사전 * instances * 벌금. – jtolle

답변

2

의도 한대로 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 
+1

나는 VBA에 교차 내장 메서드가 있다고 생각했지만 간단한 정렬 목록 함수조차도 VBA에서 얻는 것이 어렵다는 것을 알았다. –

+1

네, VBA에는 꽤 심각한 제한이 있습니다. – Stewbob

0

당신을 이 정보가 도움이 될 것입니다 :

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 
관련 문제