Expandable 2D Dictionaries?

R

Robert Dailey

Hi,

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:

myvar["cat"]["paw"] = "Some String"

The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:

myvar = {"cat": {"paw":""} }

I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).

Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

Thanks.
 
D

Diez B. Roggisch

Robert said:
Hi,

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:

myvar["cat"]["paw"] = "Some String"

The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:

myvar = {"cat": {"paw":""} }

I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).

Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

Not really, unless you know that the first level of values are _always_
dicts.

What you can do is to use

myvar.setdefault('cat', {})['paw'] = "Some String"

Diez
 
R

Robert Dailey

Robert said:
I am interested in creating an expandable (dynamic) 2D dictionary. For
example:
myvar["cat"]["paw"] = "Some String"
The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:
myvar = {"cat": {"paw":""} }
I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).
Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

Not really, unless you know that the first level of values are _always_
dicts.

What you can do is to use

myvar.setdefault('cat', {})['paw'] = "Some String"

Diez

Could you emphasize what you mean by "unless you know that the first
level of values are _always_ dicts."?

Could I create a class wrapper that automates this? I could have an
insert() method of some sort. However I would have to research if it's
possible to overload operators in python.
 
C

Carsten Haese

Hi,

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:

myvar["cat"]["paw"] = "Some String"

The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:

myvar = {"cat": {"paw":""} }

I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).

Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

Sounds like a job for Python2.5's defaultdict:

from collections import defaultdict
class magicdict(defaultdict):
def __init__(self):
self.default_factory = magicdict

myvar = magicdict()
myvar["cat"]["paw"] = "Some String"
 
M

Matimus

I'm not sure I completely understand what you want, but if you are
using Python2.5 defaultdict might be useful. Below is some example
code using defaultdict.


Code:
from collections import defaultdict

myvar = defaultdict(dict)

myvar["cat"]["paw"] = "SomeString"
myvar["dog"]["tail"] = "wags"
myvar["cat"]["tongue"] = "sand-paper"
myvar["mouse"]["likes"] = "cheese"

The documentation can do a better job of explaining how this works
than I can: http://docs.python.org/lib/defaultdict-objects.html

-Matt
 
R

Robert Dailey

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:
myvar["cat"]["paw"] = "Some String"
The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:
myvar = {"cat": {"paw":""} }
I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).
Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

This gets much easier if you change your structure around a bit:

d = {}
d["cat", "paw"] = "some string"

Jean-Paul

I like this format. I'm not familiar with it however. In my research
of python I was not aware it was legal to have comma operators inside
of the brackets. What does this mean? Is there some terminology that I
can search for to research this concept? Thanks.
 
T

Tommy Nordgren

Hi,

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:

myvar["cat"]["paw"] = "Some String"

The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:

myvar = {"cat": {"paw":""} }

I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).

Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

Thanks.
The best way to do this, is probably to implement your own class. As
far as I
know, Python supports operator overloads, although the syntax is
different than C++.
------
What is a woman that you forsake her, and the hearth fire and the
home acre,
to go with the old grey Widow Maker. --Kipling, harp song of the
Dane women
Tommy Nordgren
(e-mail address removed)
 
R

Robert Dailey

Thank you all very much for your valuable replies. I wasn't aware that
tuples were valid keys in a dictionary object. This is actually the
solution I was looking for. Thank you all once again.
 
M

Marc 'BlackJack' Rintsch

This gets much easier if you change your structure around a bit:

d = {}
d["cat", "paw"] = "some string"

Jean-Paul

I like this format. I'm not familiar with it however. In my research
of python I was not aware it was legal to have comma operators inside
of the brackets. What does this mean? Is there some terminology that I
can search for to research this concept? Thanks.

It's mapping the tuple ('cat', 'paw') to 'some string'. Commas make
tuples. The parenthesis are just necessary for the literal empty tuple
and if the syntax would be ambiguous otherwise.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steve Holden

Jean-Paul Calderone said:
Hi,

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:

myvar["cat"]["paw"] = "Some String"

The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:

myvar = {"cat": {"paw":""} }

I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).

Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

This gets much easier if you change your structure around a bit:

d = {}
d["cat", "paw"] = "some string"

Jean-Paul

Unfortunately that makes it somewhat more difficult to retrieve the set
of entries whose first index is "cat". I realise that we don't have much
in the way of use cases here, but it would seem that the original
motivation for a two-level dictionary might have been such a requirement.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
G

genro

Hi,

I am interested in creating an expandable (dynamic) 2D dictionary. For
example:

myvar["cat"]["paw"] = "Some String"

The above example assumes "myvar" is declared. In order for this to
work, I have to know ahead of time the contents of the dictionary. For
the above to work, my declaration must look like:

myvar = {"cat": {"paw":""} }

I would like to not have to declare my dictionary like this, as it
does not allow it to be expandable. I'm very new to Python (I'm a
professional C++ programmer. Any comparisons to C++ would help me
understand concepts).

Is there a way that when I index into my dictionary using an "unknown"
index (string), that python will dynamically add that key/value pair?

Thanks.

Hi Robert
take a look to our Bag module ( http://trac.genropy.org/wiki/BagManual).
Bag is a hierarchical container that can be used as nested dictionary.
If you are interested I'll send you the module.
There is not yet a public DL link as documentation has still to be
tuned...

HTH

Giovanni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top