""" The way to do defaultdicts of defaultdicts with the standard Python collections library is, e.g., foo = defaultdict(lambda: defaultdict(int))
You might want this nesting to recursively go to as many levels as necessary, which you can do like this: def recursive_dd(): return defaultdict(recursive_dd) bar = recursive_dd() This lets you assign to any depth: bar[1][2] = "hi" print bar defaultdict(, {1: defaultdict(, {2: 'hi'})})
ddict makes nested defaultdicts easier to ask for and to look at. It's used like this:
from ddict import ddict
Foo = ddict[int] Returns a CLASS called "ddict[int]" whose instances are like defaultdict(int) except for how they print. foo = ddict[int] () (Remember the parens!) gives you an instance directly. A brand new one would print as "ddict[int]({})". fooo = ddict[ddict[int]] () (Parens! Parens!) Gives you a defaultdict whose values are defaultdicts whose values are ints. Bar = ddict[...] Here we really mean three dots, the Python Ellipsis. Returns a CLASS called "ddict[...]", that acts like recursive_dd above, except for how instances print. bar = ddict[...] () (Remember the parens!) gives you an instance directly. A brand new one would print as "ddict[...]({})". bar[1][2] = "yo" print bar ddict[...]({1: ddict[...]({2: 'yo'})})
wow = ddict[ddict[ddict[int]]] ({"a": {"b": {"c": 12}}}) (Also possible with nested lambda: defaultdict's.)
Besides being prettier, the repr() strings of a ddict can be used to reconstruct the ddict, whereas an ordinary defaultdict's repr() can't. """ Once I had a simple version of this, I *had* to bring it to this state. Using ddict[class] or ddict[type] like templates-- bwaha! Using ddict[...] for ddicts all the way down-- BWAhaha!! Having the repr() be syntacticly correct-- ahhh. The plumbing to make it come out that way-- gulp, nyihh? Having to remember ddict[type] () to get an instance-- meh.
"""
ReplyDeleteThe way to do defaultdicts of defaultdicts with the standard Python
collections library is, e.g.,
foo = defaultdict(lambda: defaultdict(int))
You might want this nesting to recursively go to as many levels as
necessary, which you can do like this:
def recursive_dd():
return defaultdict(recursive_dd)
bar = recursive_dd()
This lets you assign to any depth:
bar[1][2] = "hi"
print bar
defaultdict(,
{1: defaultdict(,
{2: 'hi'})})
ddict makes nested defaultdicts easier to ask for and to look at.
It's used like this:
from ddict import ddict
Foo = ddict[int] Returns a CLASS called "ddict[int]" whose instances
are like defaultdict(int) except for how they print.
foo = ddict[int] () (Remember the parens!) gives you an instance
directly.
A brand new one would print as "ddict[int]({})".
fooo = ddict[ddict[int]] () (Parens! Parens!)
Gives you a defaultdict whose values are
defaultdicts whose values are ints.
Bar = ddict[...] Here we really mean three dots, the Python Ellipsis.
Returns a CLASS called "ddict[...]", that acts like
recursive_dd above, except for how instances print.
bar = ddict[...] () (Remember the parens!) gives you an instance
directly.
A brand new one would print as "ddict[...]({})".
bar[1][2] = "yo"
print bar
ddict[...]({1: ddict[...]({2: 'yo'})})
wow = ddict[ddict[ddict[int]]] ({"a": {"b": {"c": 12}}})
(Also possible with nested lambda: defaultdict's.)
Besides being prettier, the repr() strings of a ddict can be used to
reconstruct the ddict, whereas an ordinary defaultdict's repr() can't.
"""
Once I had a simple version of this, I *had* to bring it to this state.
Using ddict[class] or ddict[type] like templates-- bwaha!
Using ddict[...] for ddicts all the way down-- BWAhaha!!
Having the repr() be syntacticly correct-- ahhh.
The plumbing to make it come out that way-- gulp, nyihh?
Having to remember ddict[type] () to get an instance-- meh.