Sorting by item_in_another_list

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Oct 25 19:40:39 CEST 2006


"J. Clifford Dyer" <jcd at sdf.lonestar.org> wrote in message 
news:eho2kq$lqc$1 at aioe.server.aioe.org...
> 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 I'm not entirely sure what the OP wanted.  Counter example:
>
> Given these variables:
>
> A = [0,1,2,3,4,5,6,8,9,10]  # Note 7 is missing
> B = [2,3,7,8]
>
> which of the following should the function yield?
>

>From the original post:

"I have two lists, A and B, such that B is a subset of A."

So this is not a case that needs to be supported.

I envisioned something like the OP had a sequence of items to start with, 
did a random sampling from the list, and wanted to move the sampled items to 
the front of the list.

-- Paul






More information about the Python-list mailing list