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.
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
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
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]
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
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
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
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.
--
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
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
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
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,
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:
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
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
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
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:
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!
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:
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
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
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
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
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 =
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
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
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
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 =
28 matches
Mail list logo