By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
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[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.
Mar 28 '07 #1
Share this Question
Share on Google+
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[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
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[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
Mar 28 '07 #3

P: n/a
On Mar 28, 4:44 pm, <m...@fibertel.com.arwrote:
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])

Mar 29 '07 #4

P: n/a
On Mar 28, 1:44 pm, <m...@fibertel.com.arwrote:
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, ]}}

(snipped)

I think you can use, with python 2.5, defaultdict for
this. I only have access to 2.4.4 here, so my facsimile
is:

class defaultd(dict):
def __getitem__(self, k):
if k not in self:
self[k] = defaultd()
return dict.__getitem__(self, k)

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)]

dd = defaultd()

for value, outerkey, innerkey in lista:
li = dd[outerkey].setdefault(innerkey, [])
if value not in li:
li.insert(0, value)
# or, li.append(value) if you want to reverse order

print dd

I happen to like the "autovivification" feature
found in perl and was pleased to see defaultdict in
python 2.5. OTOH, python programmers more experienced
than I might see this and run away screaming.
--
Hope this helps,
Steven

Mar 29 '07 #5

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[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.

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.