how about this:
As = set([1, 2, 5, 6]) Bs = set([2, 3, 4, 5]) values = [(a, a) if a in Bs else (a, None) for a in As] + [(None, b) for b in Bs if b not in As] >>> values [(1, None), (2, 2), (5, 5), (6, None), (None, 3), (None, 4)]
or using sets:
>>> values = [(a, a) for a in As.intersection(Bs)] + [(a, None) for a in As - Bs] + [(None, b) for b in Bs - As] >>> values [(2, 2), (5, 5), (1, None), (6, None), (None, 3), (None, 4)] >>>
I am not worried about duplicates in As or B, as there will be none.
therefore, creation then gives an almost constant search time.
The order of Rs is unimportant.
so we can check A and then just check B :)
I donโt know if this is right, itโs only from the head if I have problems that I do.
UPDATE
this is a little more complicated since we cannot hash it we are basically stuck with O (n ** 2) sorry: (
I tried to make it as optimal as possible.
As = [1, 2, 5, 6] Bs = [2, 3, 4, 5] indicies_a, indicies_b, values = [], [], [] for index_a, a in enumerate(As): for index_b, b in enumerate(Bs): if cmp(a, b) == 0: values.append((a, b)) indicies_a.append(index_a) indicies_b.append(index_b) values += [(As[index], None) for index in set(range(len(As))) - set(indicies_a)] + [(None, Bs[index]) for index in set(range(len(Bs))) - set(indicies_b)] >>> values [(2, 2), (5, 5), (1, None), (6, None), (None, 3), (None, 4)] >>>
Sorry, I couldnโt come up with a more concise version, I tried my vest to do this as quickly as possible.