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



On Fri, 2007-03-30 at 14:34 -0700, erikcw wrote:
On Mar 30, 5:23 pm, FlipFish2...@xxxxxxxxx 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


.



Relevant Pages

  • Re: what does **kw mean?
    ... foobar('Shane','Geiger', x, y, adjective='useful', thing='PYTHON trick ... def method: ... National Council on Economic Education ...
    (comp.lang.python)
  • Re: Memoization and encapsulation
    ... just> I actually prefer such a global variable to the default arg ... just> trick. ... The idiom I generally use is: ... just> def func: ...
    (comp.lang.python)
  • Re: a trick with lists ?
    ... What is the use of the "" trick? ... def new_tasks1: ... Assignment to a slice of a list replaces the contents in-place. ...
    (comp.lang.python)
  • Re: n-body problem at shootout.alioth.debian.org
    ... Diff appended. ... If you want you can try that trick with this version of mine: ... You might also put the outer loop calling function advance so many ... def advance: ...
    (comp.lang.python)
  • Re: [ANN] Turing 0.0.7 && cry for help
    ... > You can patch rubygems/builder.rb with ... the trick is overriding to_yaml method: ... def spec.to_yaml ...
    (comp.lang.ruby)