Is there any particular reason that my application says the argument must be callable or none type? I'm pretty sure this is how you'd instantiate a defaultdict with a defaultdict as its values.
dict = defaultdict(defaultdict(set))
The argument provided to the defaultdict
constructor must be a zero-argument callable (see the docs ), so defaultdict(defaultdict)
does work, but defaultdict(defaultdict(set))
does not. You can 'cheat' a little though:
d = defaultdict(lambda: defaultdict(set))
That way you provide a zero-argument callable in the form of the lambda
function which in turn, when called, returns the appropriate default value you want.
You need to provide a function for defaultdict
constructor but defauldict(set)
is defaultdict
object instead. If you want to build a defaultdict
whose values are defaultdicts
you can use lambdas:
from collections import defaultdict
dd = lambda: defaultdict(dd)
x = dd()
x['foo']['bar']['foobar'] = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.