Re: Creating a new data structure while filtering its data origin.
On Mar 28, 4:44 pm, <[EMAIL PROTECTED]> wrote: > Hi everyone. > > I'm trying to work with very simple data structures but I'm stuck in the very > first steps. If someone has the luxury of a few minutes and can give an > advice how to resolve this, I'll really appreciate it. > > 1- I have a list of tuples like this: > lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, > 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, > 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, > 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)] > and I want to end with a dict like this: > {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], > 2: [568, ], 4: [562, ]}} > the logic of the final output: > a) the outer dict's key is a set() of the 2rd value of the input. > b) the inner dict's key is a set() of the 3th value for tuples which 3rd > value equals a). > c) the inner list will be fill up with the 1st value of every tuple which > 3rd value equals b) and its 2rd value equals a). > > So far, the only thing it seems I can achieve is the first part: > outer_dict = dict([(x,dict()) for x in set(row[1] for row in lista)]) > > >From then on, I'm starting to get tired after several successful failures (I > >tried with itertools, with straight loops ...) and I don't know which can be > >the easier way to get that final output. > > Thanks in advance. dict([[b,dict([[k, dict([[x, None] for (x,y,z) in lista if y==b and z==k]).keys()] for (i,j,k) in lista if j==b])] for (a,b,c) in lista]) -- http://mail.python.org/mailman/listinfo/python-list
Re: I can't get multi-dimensional array to work...
On Mar 30, 4:56 pm, "erikcw" <[EMAIL PROTECTED]> wrote: > Hi, > > I'm trying to create a multidimensional data structure. However, id > doesn't seem to work past the 2nd dimension. > > Here is my method: > > def endElement(self, name): > if name == 'row' : > if not self.data.has_key(self.parent): > self.data[self.parent] = {} > elif 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)) > > I've tried all kinds of different variations, and I keep getting the > same result: > > Traceback (most recent call last): > File "sax.py", line 123, in ? > parser.parse(open('report.xml')) > File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py", > line 109, in parse > xmlreader.IncrementalParser.parse(self, source) > File "/usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py", > line 123, in parse > self.feed(buffer) > File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py", > line 216, in feed > self._parser.Parse(data, isFinal) > File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py", > line 315, in end_element > self._cont_handler.endElement(name) > File "sax.py", line 51, in endElement > self.data[self.parent][self.child].append((self.creativeid, > self.clicks, self.imps)) > KeyError: u'Pickup Trucks' > > I have a lot of php experience - am I accidentally doing a "php thing" > in my code? > > Thanks so much for your help! > > Erik 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)) -- http://mail.python.org/mailman/listinfo/python-list