I am currently using a defaultdict
of Counter
to uniquely count several unpredictable values for unpredictable keys:
from collections import defaultdict, Counter
d = defaultdict(Counter)
d['x']['b'] += 1
d['x']['c'] += 1
print(d)
This gives me the expected result:
defaultdict(<class 'collections.Counter'>, {'x': Counter({'c': 1, 'b': 1})})
I now need to expand the structure of the values in the defaultdict
and make it a dict
with two keys: the previous Counter
and an str
:
mystruct = {
'counter': collections.Counter(),
'name': ''
}
Is it possible to use a specific data structure (like the above) as the default_factory
in defaultdict
? The expected result would be that for each nonexistent key in the defaultdict
, a new key and value initialized with the structure above would be created.