我试图在VBA中模仿我编写的Python脚本,它动态地创建了dicitonary。我在以下循环的最后一行遇到问题:
Dim transactions As New Scripting.dictionary
Dim used_parents As collection
Set used_parents = New collection
Dim parent_count As Integer
Dim groups As New Scripting.dictionary
[...]
For Each Key In transactions.Keys
used_parents.Add transactions(Key)
parent_count = col_item_count(transactions(Key), used_parents)
groups(parent_count).Add Key' this does not work; I need to somehow tell VBA that "groups(parent_count)" is a collection, to be able to add to that collection
Next Key
我需要groups(parent_count)
字典值作为集合才能附加到它。
VBA中是否可以将集合作为字典值?
作为参考,工作的Python代码看起来像这样(字典中的键是列表,所以我可以很容易地附加到它们):
from collections import defaultdict
partials = ['tomek','jacek','agata','piotrek','stefan','kasia','krystyna','janek','pawel','leopold']
parents = ['a','b','c','d','a','d','f','c','c','a']
transactions = zip(partials, parents)
used_parents = []
groups = defaultdict(List)
for key, value in transactions:
used_parents.append (value)
parent_count = used_parents.Count(value)
groups ['Group ' + str(parent_count)].append(key)