444,058 Members | 1,213 Online Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

# Creating a new data structure while filtering its data origin.

 P: n/a 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 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. Mar 28 '07 #1
5 Replies

 P: n/a mm***@fibertel.com.ar 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 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 Mar 28 '07 #2

 P: n/a mm***@fibertel.com.ar 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 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 Mar 28 '07 #3

 P: n/a On Mar 28, 4:44 pm,

 P: n/a On Mar 28, 1:44 pm,

 P: n/a mm***@fibertel.com.ar 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 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. lista, wanted = ... result = {} for a,b,c in lista: inner = result.setdefault(b, {}).setdefault(c, []) if a not in inner: inner.insert(0, a) # I had used append, but ... print result == wanted --Scott David Daniels sc***********@acm.org Mar 30 '07 #6

### This discussion thread is closed

Replies have been disabled for this discussion. 