Initialise dictionary of dictionary
Ayushi Dalmia
ayushidalmia2604 at gmail.com
Fri Jan 24 00:42:39 EST 2014
Thank you so much Tim. This is precisely what I wanted to do!
On Thursday, January 23, 2014 9:00:23 PM UTC+5:30, Tim Chase wrote:
> On 2014-01-23 07:15, Ayushi Dalmia wrote:
>
> > I need to initialise a dictionary of dictionary with float values.
>
> > I do not know the size of the dictionary beforehand. How can we do
>
> > that in Python --
>
>
>
> Either
>
>
>
> d = {}
>
>
>
> or, if you want
>
>
>
> from collections import defaultdict
>
> d = defaultdict(float)
>
> print(d["Hello"])
>
>
>
> If you really do want a dict-of-dict that defaults to floats, you can
>
> do
>
>
>
> d = defaultdict(lambda: defaultdict(float))
>
> print(d[3141]["Hello"])
>
>
>
> -tkc
More information about the Python-list
mailing list