Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64661

Re: Initialise dictionary of dictionary

Newsgroups comp.lang.python
Date 2014-01-23 21:42 -0800
References <f2f630c9-434c-43c1-bd80-4fadcc552e0a@googlegroups.com> <mailman.5894.1390490976.18130.python-list@python.org>
Message-ID <b5baa106-15a6-4e9a-bbfc-27c3224510ed@googlegroups.com> (permalink)
Subject Re: Initialise dictionary of dictionary
From Ayushi Dalmia <ayushidalmia2604@gmail.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Initialise dictionary of dictionary Ayushi Dalmia <ayushidalmia2604@gmail.com> - 2014-01-23 07:15 -0800
  Re: Initialise dictionary of dictionary Tim Chase <python.list@tim.thechases.com> - 2014-01-23 09:30 -0600
    Re: Initialise dictionary of dictionary Ayushi Dalmia <ayushidalmia2604@gmail.com> - 2014-01-23 21:42 -0800
  Re:Initialise dictionary of dictionary Dave Angel <davea@davea.name> - 2014-01-23 10:34 -0500
  Re: Initialise dictionary of dictionary Tim Chase <python.list@tim.thechases.com> - 2014-01-23 09:41 -0600

csiph-web