Re: Sorting by item_in_another_list

2006-10-26 Thread Cameron Walsh
Paul McGuire wrote: J. Clifford Dyer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] ZeD wrote: Paul Rubin wrote: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about: desired_result = B + sorted(x for x in A if x not in B) this.

Re: Sorting by item_in_another_list

2006-10-26 Thread Steven Bethard
Cameron Walsh wrote: Which brings me to the question, would this solution: B = set(B) A = B + list(x for x in A if x not in B) be faster than this solution: B = set(B) A.sort(key=B.__contains__, reverse=True) My guess is yes, since while the __contains__ method is only run once

Re: Sorting by item_in_another_list

2006-10-26 Thread Paul Rubin
Steven Bethard [EMAIL PROTECTED] writes: Cameron Walsh wrote: Which brings me to the question, would this solution: B = set(B) A = B + list(x for x in A if x not in B) be faster than this solution: B = set(B) A.sort(key=B.__contains__, reverse=True) [timings deleted] That said, I'd

Re: Sorting by item_in_another_list

2006-10-26 Thread Steven Bethard
Paul Rubin wrote: Steven Bethard [EMAIL PROTECTED] writes: Cameron Walsh wrote: Which brings me to the question, would this solution: B = set(B) A = B + list(x for x in A if x not in B) be faster than this solution: B = set(B) A.sort(key=B.__contains__, reverse=True) [timings deleted]

Re: Sorting by item_in_another_list

2006-10-26 Thread Cameron Walsh
Steven Bethard wrote: As you noted, you'll get an error if you try to concatenate B as a set to the list. Steve Whoops, remind me to check things work before I type them. In the mean time, here are some more interesting timing results: With a larger data set, 500 elements instead of

Re: Sorting by item_in_another_list

2006-10-25 Thread Fredrik Lundh
Delaney, Timothy (Tim) wrote: That says pretty strongly to me that it's part of the language specification. And I'm pretty sure Guido said as much when he pronounced. oops. I'll blame it all on google's ability to bring up the wrong page on python.org when you do a quick google. guess I

Re: Sorting by item_in_another_list

2006-10-25 Thread ZeD
Paul Rubin wrote: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about: desired_result = B + sorted(x for x in A if x not in B) this. is. cool. -- Under construction -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting by item_in_another_list

2006-10-25 Thread Paul Rubin
ZeD [EMAIL PROTECTED] writes: desired_result = B + sorted(x for x in A if x not in B) this. is. cool. Actually it's better to use a set if B is large: B2 = set(B) desired_result = B + sorted(x for x in A if x not in B2) That avoids a linear search through B for each element of A. --

Re: Sorting by item_in_another_list

2006-10-25 Thread Fredrik Lundh
Paul Rubin wrote: for example: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about: desired_result = B + sorted(x for x in A if x not in B) assuming that keep the existing order means what it says, you might as well replace sorted with a

Re: Sorting by item_in_another_list

2006-10-25 Thread Paul Rubin
Fredrik Lundh [EMAIL PROTECTED] writes: desired_result = B + sorted(x for x in A if x not in B) assuming that keep the existing order means what it says, you might as well replace sorted with a list comprehension. Hmm. I didn't read it that way, but yeah, if that's what the OP meant then

Re: Sorting by item_in_another_list

2006-10-25 Thread J. Clifford Dyer
ZeD wrote: Paul Rubin wrote: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about: desired_result = B + sorted(x for x in A if x not in B) this. is. cool. Cool, yes, but I'm not entirely sure it does what the OP wanted. Partly because

Re: Sorting by item_in_another_list

2006-10-25 Thread Paul McGuire
J. Clifford Dyer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] ZeD wrote: Paul Rubin wrote: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about: desired_result = B + sorted(x for x in A if x not in B) this. is. cool. Cool,

Re: Sorting by item_in_another_list

2006-10-25 Thread Paul McGuire
Paul McGuire [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] J. Clifford Dyer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] ZeD wrote: Paul Rubin wrote: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about:

Re: Sorting by item_in_another_list

2006-10-25 Thread J. Clifford Dyer
Right you are. I was a bit hasty. Many thanks. Cliff Paul McGuire wrote: From the original post: I have two lists, A and B, such that B is a subset of A. -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting by item_in_another_list

2006-10-24 Thread Cameron Walsh
Delaney, Timothy (Tim) wrote: Cameron Walsh wrote: Hi, I have two lists, A and B, such that B is a subset of A. I wish to sort A such that the elements in B are at the beginning of A, and keep the existing order otherwise, i.e. stable sort. The order of elements in B will always be

Re: Sorting by item_in_another_list

2006-10-24 Thread Gabriel Genellina
At Tuesday 24/10/2006 04:35, Cameron Walsh wrote: c = set(B) a.sort(key=c.__contains__, reverse=True) Tim Delaney Depressingly simple. I like it. ...and fast! -- Gabriel Genellina Softlab SRL __ Correo Yahoo! Espacio para todos tus

Re: Sorting by item_in_another_list

2006-10-24 Thread Carsten Haese
On Tue, 2006-10-24 at 11:31, Gabriel Genellina wrote: At Tuesday 24/10/2006 04:35, Cameron Walsh wrote: c = set(B) a.sort(key=c.__contains__, reverse=True) Tim Delaney Depressingly simple. I like it. ...and fast! ...and not guaranteed to be correct:

Re: Sorting by item_in_another_list

2006-10-24 Thread Carsten Haese
On Tue, 2006-10-24 at 11:41, Carsten Haese wrote: On Tue, 2006-10-24 at 11:31, Gabriel Genellina wrote: At Tuesday 24/10/2006 04:35, Cameron Walsh wrote: c = set(B) a.sort(key=c.__contains__, reverse=True) Tim Delaney Depressingly simple. I like it. ...and fast!

Re: Sorting by item_in_another_list

2006-10-24 Thread Peter Otten
Carsten Haese wrote: On Tue, 2006-10-24 at 11:31, Gabriel Genellina wrote: At Tuesday 24/10/2006 04:35, Cameron Walsh wrote: c = set(B) a.sort(key=c.__contains__, reverse=True) Tim Delaney Depressingly simple. I like it. ...and fast! ...and not guaranteed to be correct:

Re: Sorting by item_in_another_list

2006-10-24 Thread bearophileHUGS
Carsten Haese: However, it's not clear whether this specifies language behavior that all implementations must adhere to, or whether it simply documents an implementation detail of CPython. I think it's CPython-specific, but maybe here people expert in Jython and IronPython can tell if their

RE: Sorting by item_in_another_list

2006-10-24 Thread Delaney, Timothy (Tim)
Carsten Haese wrote: The current documentation states that Starting with Python 2.3, the sort() method is guaranteed to be stable. However, it's not clear whether this specifies language behavior that all implementations must adhere to, or whether it simply documents an implementation detail

Re: Sorting by item_in_another_list

2006-10-24 Thread Fredrik Lundh
Delaney, Timothy (Tim) wrote: This is a requirement for all implementations claiming to be 2.3 or higher. the language reference only guarantees this for CPython: The C implementation of Python 2.3 introduced a stable sort() method, but code that intends to be portable across

RE: Sorting by item_in_another_list

2006-10-24 Thread Delaney, Timothy (Tim)
Fredrik Lundh wrote: Delaney, Timothy (Tim) wrote: This is a requirement for all implementations claiming to be 2.3 or higher. the language reference only guarantees this for CPython: The C implementation of Python 2.3 introduced a stable sort() method, but code that intends

Re: Sorting by item_in_another_list

2006-10-23 Thread Cameron Walsh
Cameron Walsh wrote: Hi, I have two lists, A and B, such that B is a subset of A. I wish to sort A such that the elements in B are at the beginning of A, and keep the existing order otherwise, i.e. stable sort. The order of elements in B will always be correct. for example: A =

Re: Sorting by item_in_another_list

2006-10-23 Thread Paul McGuire
Cameron Walsh [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, I have two lists, A and B, such that B is a subset of A. I wish to sort A such that the elements in B are at the beginning of A, and keep the existing order otherwise, i.e. stable sort. The order of elements in

Re: Sorting by item_in_another_list

2006-10-23 Thread Cameron Walsh
Paul McGuire wrote: Cameron Walsh [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, I have two lists, A and B, such that B is a subset of A. I wish to sort A such that the elements in B are at the beginning of A, and keep the existing order otherwise, i.e. stable sort. The

Re: Sorting by item_in_another_list

2006-10-23 Thread Paul Rubin
Cameron Walsh [EMAIL PROTECTED] writes: for example: A = [0,1,2,3,4,5,6,7,8,9,10] B = [2,3,7,8] desired_result = [2,3,7,8,0,1,4,5,6,9,10] How about: desired_result = B + sorted(x for x in A if x not in B) -- http://mail.python.org/mailman/listinfo/python-list

RE: Sorting by item_in_another_list

2006-10-23 Thread Delaney, Timothy (Tim)
Cameron Walsh wrote: Hi, I have two lists, A and B, such that B is a subset of A. I wish to sort A such that the elements in B are at the beginning of A, and keep the existing order otherwise, i.e. stable sort. The order of elements in B will always be correct. for example: A =