An ordering question

2009-03-13 Thread 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[i] = (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?

--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread MRAB

Kottiyath wrote:

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[i] = (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], 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. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread MRAB

MRAB wrote:

Kottiyath wrote:

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[i] = (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], 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[s] for s in b]
 c
[(3, 2), (2, 4), (4, 1), (7, 3)]
--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread Hrvoje Niksic
Kottiyath n.kottiy...@gmail.com writes:

 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)
--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread andrew cooke
Hrvoje Niksic wrote:
 Kottiyath n.kottiy...@gmail.com writes:

 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)

that's hilarious!  i have no idea how it works, but unfortunately it's not
general:

 a = [(4, 1), (7, 3), (3, 2), (2, 4)]
 b = [4, 2, 1, 3]
 a.sort(key=lambda xy: b[xy[1] - 1], reverse=True)
 a
[(4, 1), (2, 4), (3, 2), (7, 3)]

(in 3.0 there's no unpacking in function args)

did you just play around until you got the right answer?  how many
different tries did you need?

the answer i think the original person was looking for is:

 a = [(4, 1), (7, 3), (3, 2), (2, 4)]
 b = [2, 4, 1, 3]
 a.sort(key=lambda xy: b.index(xy[1]))
 a
[(3, 2), (2, 4), (4, 1), (7, 3)]

 a = [(4, 1), (7, 3), (3, 2), (2, 4)]
 b = [4, 2, 1, 3]
 a.sort(key=lambda xy: b.index(xy[1]))
 a
[(2, 4), (3, 2), (4, 1), (7, 3)]

however, it's not very efficient.  it would be better to do:

 a = [(4, 1), (7, 3), (3, 2), (2, 4)]
 b = [4, 2, 1, 3]
 idx = dict((b[i], i) for i in range(len(b)))
 a.sort(key=lambda xy: idx[xy[1]])
 a
[(2, 4), (3, 2), (4, 1), (7, 3)]

for large lists

andrew


--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread andrew cooke
MRAB wrote:
   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], s) for s in b]
   c
 [(3, 2), (2, 4), (4, 1), (7, 3)]

ah, that is more efficient than the suggestions i posted.

andrew


--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread Hrvoje Niksic
andrew cooke and...@acooke.org writes:

 Hrvoje Niksic wrote:
 Kottiyath n.kottiy...@gmail.com writes:

 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)

 that's hilarious!  i have no idea how it works
[...]
 did you just play around until you got the right answer?  how many
 different tries did you need?

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.

Your solution are probably the two lines the OP was looking for.
--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread John Posner

 If you don't want to build the intermediary dict, a
 less efficient
 version that runs in O(n^2):
 
 a.sort(key=lambda k: b.index(k[1]))
 
 Which is mostly similar to John's solution, but still
 more efficient
 because it only does a b.index call once per 'a'
 item instead of twice
 per comparison.
 

Very nice! This solution is pretty much a direct transliteration of the 
original problem, as the OP *might* have articulated it:

  List A consists of ordered pairs. Sort list A according to the
  position in list B of the pair's second member.

A solution is truly Pythonic when it possesses this directness.

-John

--
http://mail.python.org/mailman/listinfo/python-list


Parameter sublists [was: An ordering question]

2009-03-13 Thread Peter Pearson
On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic hnik...@xemacs.org wrote:
[snip]
 a.sort(key=lambda (x, y): b[y - 1], reverse=True)

Huh?  I had no idea one could do this:

 def g( ( ( x, y ), z ) ):
...   return y
... 
 g( ((1,2),3) )
2

What should I have read to learn that trick?

-- 
To email me, substitute nowhere-spamcop, invalid-net.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parameter sublists [was: An ordering question]

2009-03-13 Thread Chris Rebert
On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson ppear...@nowhere.invalid wrote:
 On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic hnik...@xemacs.org wrote:
 [snip]
 a.sort(key=lambda (x, y): b[y - 1], reverse=True)

 Huh?  I had no idea one could do this:

 def g( ( ( x, y ), z ) ):
 ...   return y
 ...
 g( ((1,2),3) )
 2

 What should I have read to learn that trick?

Don't bother. It's been excised in Python 3.0.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Parameter sublists [was: An ordering question]

2009-03-13 Thread Kottiyath
On Mar 14, 5:39 am, Chris Rebert c...@rebertia.com wrote:
 On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson ppear...@nowhere.invalid 
 wrote:
  On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic hnik...@xemacs.org 
  wrote:
  [snip]
  a.sort(key=lambda (x, y): b[y - 1], reverse=True)

  Huh?  I had no idea one could do this:

  def g( ( ( x, y ), z ) ):
  ...   return y
  ...
  g( ((1,2),3) )
  2

  What should I have read to learn that trick?

 Don't bother. It's been excised in Python 3.0.

 Cheers,
 Chris

 --
 I have a blog:http://blog.rebertia.com

Thank you very much.
These many solutions ?
I think programming is not my forte :-) :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: An ordering question

2009-03-13 Thread Gabriel Genellina
En Fri, 13 Mar 2009 17:33:51 -0200, Hrvoje Niksic hnik...@xemacs.org  
escribió:

andrew cooke and...@acooke.org writes:

Hrvoje Niksic wrote:

Kottiyath n.kottiy...@gmail.com writes:


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.


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list