Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64615 > unrolled thread
| Started by | Ayushi Dalmia <ayushidalmia2604@gmail.com> |
|---|---|
| First post | 2014-01-23 07:15 -0800 |
| Last post | 2014-01-23 09:41 -0600 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Ayushi Dalmia <ayushidalmia2604@gmail.com> |
|---|---|
| Date | 2014-01-23 07:15 -0800 |
| Subject | Initialise dictionary of dictionary |
| Message-ID | <f2f630c9-434c-43c1-bd80-4fadcc552e0a@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-01-23 09:30 -0600 |
| Message-ID | <mailman.5894.1390490976.18130.python-list@python.org> |
| In reply to | #64615 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ayushi Dalmia <ayushidalmia2604@gmail.com> |
|---|---|
| Date | 2014-01-23 21:42 -0800 |
| Message-ID | <b5baa106-15a6-4e9a-bbfc-27c3224510ed@googlegroups.com> |
| In reply to | #64617 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-01-23 10:34 -0500 |
| Message-ID | <mailman.5895.1390491122.18130.python-list@python.org> |
| In reply to | #64615 |
Ayushi Dalmia <ayushidalmia2604@gmail.com> Wrote in message: > 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 > Do what? There's no concept of pre-initializing a dictionary, and there's no specific limit to its eventual size. Unsure of what the floats have to do with it. Perhaps you meant float KEYS. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-01-23 09:41 -0600 |
| Message-ID | <mailman.5896.1390491617.18130.python-list@python.org> |
| In reply to | #64615 |
On 2014-01-23 10:34, Dave Angel wrote: > Unsure of what the floats have to do with it. Perhaps you meant > float KEYS. using floats for keys can be dangerous, as small rounding errors in math can produce keys different enough that they're not found by an exact-match lookup. But yeah, the original problem specification was rather wanting in detail. I tried my best at interpreting it, for whatever that may be worth. -tkc
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web