Complex datastructures in Python

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Jun 28 04:14:31 EDT 2000


Greg Gallagher wrote in comp.lang.python: 
> I'm a little disheartened here... I know how to do this in Perl, but I'm lost in Python and 
> searching for 'complex data structures' is landing me nowhere.  Basically,  I want
> a dictionary whosevalues are lists of dictionaries.  Simple.
> I want to basically loop through some data and so something like this:
> 
> while loop:
>    new_forward = {'ip' : ip_address,
>                   'hostname' : hostname,
>                   'mac_address' : mac_address}
> 
     if not forward.has_key(domain):
	    forward[domain] = []
>    forward[domain].append(new_forward)

The only problem is that you can't append to it if it's not a list yet,
so you have to initialize forward[domain] to the empty list first.

(btw, this is not a hash of hashes, but a hash of lists of hashes, right?)

> Any pointers?  Also, any good documentation for 'complex datastructures',
> ala Perl's Cookbook ?

I've never had any problem with complex structures in Python, really. They
tend to just work. If they become too complex wrapping them in some OO
usually helps. 

A Python version of Perl's Cookbook may or not be useful (check Deja - it's
been discussed before - someone also mentioned that O'Reilly is actually
working on it). "Python Annotated Archives" is the book that comes
closest I think, but it's had mixed reviews - check Deja again, I suppose.

But just use Python for a while. It's consistent, there's not that much to
learn.

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  Murphy's Rules, "Gblrsh smrzl, Mom 'n' Dad":
   In Runequest III (Avalon Hill), two people from the same village,
   speaking the same language, have a 1 in 3 chance of totally
   misunderstanding each other.



More information about the Python-list mailing list