Suppose I have two lists:
a = ['30', '10', '90', '1111', '17']
b = ['60', '1201', '30', '17', '900']
How would you sort this most efficiently, such that:
list b
is sorted with respect to a
. Unique elements in b
should be placed at the end of the sorted list. Unique elements in a
can be ignored.
example output:
c = ['30', '17', '60', '1201', '900']
Sorry, it's a simple question. My attempt is stuck at the point of taking the intersection.
intersection = sorted(set(a) & set(b), key = a.index)
There is no need to actually sort here. You want the elements in a
which are in b
, in the same order as they were in a
; followed by the elements in b
which are not in a
, in the same order as they were in b
.
We can just do this with two filters, using the sets for fast membership tests:
>>> a = ['30', '10', '90', '1111', '17']
>>> b = ['60', '1201', '30', '17', '900']
>>> a_set = set(a)
>>> b_set = set(b)
>>> [*filter(lambda x: x in b_set, a), *filter(lambda x: x not in a_set, b)]
['30', '17', '60', '1201', '900']
Or if you prefer comprehensions:
>>> [*(x for x in a if x in b_set), *(x for x in b if x not in a_set)]
['30', '17', '60', '1201', '900']
Both take linear time, which is better than sorting.