[Tutor] Multi-Dimensional Dictionary that contains a 12 element list.

Paul Kraus pkraus at pelsupply.com
Wed Dec 28 16:18:13 CET 2005


I am trying to build a data structure that would be a dictionary of a 
dictionary of a list.

In Perl I would build the structure like so $dictionary{key1}{key2}[0] = X
I would iterate like so ...
foreach my $key1 ( sort keys %dictionary ) {
	foreach my $key2 ( sort keys %{$dictionary{$key1}} ) {
		foreach my $element ( @{$dictionary{$key1}{$key2} } ) {
			print "$key1 => $key2 => $element\n";
		}
	}
}

Sorry for the Perl reference but its the language I am coming from. I use data 
structures like this all the time. I don't always iterate them like this but 
If i can learn to build these and move through them in python then a good 
portion of the Perl apps I am using can be ported.

Playing around I have come up with this but have no clue how to iterate over 
it or if its the best way. It seems "clunky" but it is most likely my lack of 
understanding.

dictionary[(key1,key2)]=[ a,b,c,d,e,f,g ... ]

This i think gives me a dictionary with two keys ( not sure how to doing 
anything usefull with it though) and a list.

Now I am not sure how I can assign one element at a time to the list.

here is the pseudo code.
read text file.
split line from text file into list of fields.
One of the fields contains the date. Split the date into two fields Year and 
Month/Period. Build data structure that is a dictionary based on year, based 
on period, based on item code then store/increment the units sold based on 
period.

dictionary[(year,period)] = [ jan, feb, mar, apr, may, jun, july, aug, sep, 
oct, nov ,dec]

I would prefer to have the months just be an array index 0 through 11 and when 
it reads the file it increments the number contained there.

TIA,
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=


More information about the Tutor mailing list