Complex datastructures in Python

Eric Lee Green eric at estinc.com
Tue Jun 27 19:27:17 EDT 2000


Greg Gallagher wrote:
> 
> 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}
> 
>    forward[domain].append(new_forward)

You may want to simply this by using classes. For example, here's a class
"IP_Address":

class IP_Address:
    def __init__(self,ip,hostname,mac_address):
         self.ip=ip
         self.hostname=hostname
         self.mac_address=mac_address

Then:
      new_forward=IP_Address(ip_address,hostname,mac_address)

Then:

      masq_table['forward'].append(new_forward)

then later:

   for ip in masq_table['forward']:
       ...do something here...


-- 
Eric Lee Green                         eric at estinc.com
Software Engineer                      Visit our Web page:
Enhanced Software Technologies, Inc.   http://www.estinc.com/
(602) 470-1115 voice                   (602) 470-1116 fax



More information about the Python-list mailing list