[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. > >
d={} for a, b, c in lista: if d.has_key(b): if d[b].has_key(c): if a not in d[b][c]: d[b][c].append(a) else: d[b][c]=[a] else: d[b]={c:[a]} print d -Larry Bates -- http://mail.python.org/mailman/listinfo/python-list