I can't get multi-dimensional array to work...

Carsten Haese carsten at uniqsys.com
Fri Mar 30 21:55:42 EDT 2007


On Fri, 2007-03-30 at 14:34 -0700, erikcw wrote:
> On Mar 30, 5:23 pm, FlipFish2... at yahoo.com wrote:
> > I haven't tested it, but superficially I'd suggest giving this a try:
> >
> > def endElement(self, name):
> >         if name == 'row' :
> >             if not self.data.has_key(self.parent):
> >                 self.data[self.parent] = {}
> >             if not self.data[self.parent].has_key(self.child):
> >                 self.data[self.parent][self.child] = []
> >             self.data[self.parent]
> > [self.child].append((self.creativeid, self.clicks, self.imps))
> 
> That seems to have done the trick!  I can't believe I spent all
> afternoon trying to figure that out!!  Thanks a million!

Since you're already using dictionaries, as an alternative solution you
could construct a single dictionary that is keyed on two-dimensional
tuples (also known as ordered pairs). Combine that with the setdefault
method to initialize new entries and you end up with something like
this:

def endElement(self, name):
   if name == 'row':
      entry = self.data.setdefault((self.parent,self.child),[])
      entry.append(...)

-Carsten





More information about the Python-list mailing list