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


Groups > comp.lang.python > #64617

Re: Initialise dictionary of dictionary

Date 2014-01-23 09:30 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Initialise dictionary of dictionary
References <f2f630c9-434c-43c1-bd80-4fadcc552e0a@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5894.1390490976.18130.python-list@python.org> (permalink)

Show all headers | View raw


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