Sorting by item_in_another_list
Paul Rubin
http
Thu Oct 26 04:00:14 EDT 2006
Steven Bethard <steven.bethard at gmail.com> 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 probably still use the first solution -- it's more
> immediately obvious why that one works.
Wait a minute, the first example looks wrong, B has gotten replaced by
a set and then it's added to a list.
Anyway how about timing
C = set(A) - set(B)
A = B + filter(C.__contains__, A)
This scans A twice, but it does more of the work in native code,
without sorting.
More information about the Python-list
mailing list