An ordering question

K

Kottiyath

Hi,
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]

Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]

I did the same as follows:
l = len(a) * [None]
for (k, v) in a:
.... for i, e in enumerate(b):
.... if e == v:
.... l = (k, v)

This works, but the code -for python- looks very kludgy.
I thought for ~2 hours to see whether I can do it in a line or 2,
but I cannot seem to find a mechanism.
Can someone help me out?
 
M

MRAB

Kottiyath said:
Hi,
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]

Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]

I did the same as follows:
l = len(a) * [None]
for (k, v) in a:
... for i, e in enumerate(b):
... if e == v:
... l = (k, v)

This works, but the code -for python- looks very kludgy.
I thought for ~2 hours to see whether I can do it in a line or 2,
but I cannot seem to find a mechanism.
Can someone help me out?

How about:
>>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
>>> b = [2, 4, 1, 3]
>>> d = dict((v, k) for k, v in a)
>>> c = [(d, s) for s in b]
>>> c

[(3, 2), (2, 4), (4, 1), (7, 3)]

Understanding how it works is left as an exercise for the reader. :)
 
M

MRAB

MRAB said:
Kottiyath said:
Hi,
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]

Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]

I did the same as follows:
l = len(a) * [None]
for (k, v) in a:
... for i, e in enumerate(b):
... if e == v:
... l = (k, v)

This works, but the code -for python- looks very kludgy.
I thought for ~2 hours to see whether I can do it in a line or 2,
but I cannot seem to find a mechanism.
Can someone help me out?

How about:
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]
d = dict((v, k) for k, v in a)
c = [(d, s) for s in b]
c

[(3, 2), (2, 4), (4, 1), (7, 3)]

Understanding how it works is left as an exercise for the reader. :)

Actually, a more general solution is:
>>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
>>> b = [2, 4, 1, 3]
>>> d = dict((t[1], t) for t in a)
>>> c = [d for s in b]
>>> c

[(3, 2), (2, 4), (4, 1), (7, 3)]
 
H

Hrvoje Niksic

Kottiyath said:
Hi,
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]

Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)] [...]
whether I can do it in a line or 2,

a.sort(key=lambda (x, y): b[y - 1], reverse=True)
 
G

Gabriel Genellina

andrew cooke said:
Hrvoje said:
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]
Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]

a.sort(key=lambda (x, y): b[y - 1], reverse=True)

that's hilarious! i have no idea how it works

Glad to have made someone laugh, but it in fact wasn't meant to. :)
It was a combination of misunderstanding the question *and* trying out
the answer.

I understood that the second element of each tuple should be sorted
according to the corresponding element in b. This part is partly
correct, but I took corresponding to mean that it should be sorted by
the value of b[x], x being the second element of each tuple. Since
those elements appear to be 1-based, I added -1. This produced a list
in exactly the reverse order, so I added reverse=True.

This shows how important is to provide a good, unambiguous example in any
specification. Not just a "correct" example, but one that also shows how
things are not to be done. In case of doubt, people always look at the
examples for clarification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top