4

Case:

a = [(1,2),(2,3),(4,5),(1,6),(1,7)]
b = [(5,2),(6,3),(4,5),(6,8),(1,9)]

How can i remove duplicates by first tuple item?

Result for a would be:

[(1,2),(2,3),(4,5)]

Result for b would be:

[(5,2),(6,3),(4,5),(1,9)]

How can i merge both without duplicates?: Result would be:

[(1,2),(2,3),(4,5),(5,2),(6,3)]

How can i get the intersection of both?: Result would be:

[(1,2),(4,5)]

Is this in an easy way possible?

Best Regards Chris

user2543694
  • 73
  • 1
  • 5
  • Why is (1, 2) in the intersection? Do we say that x is an element of the intersection of A and B if both A and B have an element with the same "key" (first element of the pair) as x? And if so, which second element do we take? – Caleb Jul 02 '13 at 17:59
  • possible duplicate http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order – Matthew Plourde Jul 02 '13 at 18:06
  • @CalebLevine Because i need the Intersection by the first Element of every tuple item. So in a and b start two tuples with 1 ( (1,9) in a and (1,2) in b). Intersection would be (1,9) or (1,2) ... (4,5) too! – user2543694 Jul 02 '13 at 18:26
  • What is the expected value for intersection? – Ashwini Chaudhary Jul 02 '13 at 18:28

2 Answers2

1

Use sets:

>>> seen = set()
>>> s1 = [x for x in a if x[0] not in seen and not seen.add(x[0])]
>>> seen = set()
>>> s2 = [x for x in b if x[0] not in seen and not seen.add(x[0])]
>>> s1
[(1, 2), (2, 3), (4, 5)]
>>> s2
[(5, 2), (6, 3), (4, 5), (1, 9)]

Union:

>>> from itertools import chain
>>> seen = set()
>>> [x for x in chain(s1,s2) if x[0] not in seen and not seen.add(x[0])]
[(1, 2), (2, 3), (4, 5), (5, 2), (6, 3)]

Intersection:

>>> se1 = set(x[0] for x in s1)
>>> se2 = set(x[0] for x in s2)
>>> inter = se1 & se2
>>> inter
set([1, 4])
>>> seen = set()
>>> [x for x in chain(s1,s2) if x[0] in inter and x[0] not in seen
                                                          and not seen.add(x[0])]
[(1, 2), (4, 5)]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Thank you very much! The first one is exactly what i want. The Union and Intersection should be referenced to first item of the Tuple too (how in the first example).The Intersection result of duplicates free a and b should be: [(1,2), (1,9)]. For Union the same. – user2543694 Jul 02 '13 at 18:13
  • @user2543694 intersection should be `[(1,2),(4,5)]`, as per what you posted in question body. – Ashwini Chaudhary Jul 02 '13 at 18:18
0

Here is simple Python3 example for removing duplicates. Another contributor covered the other two.

ixs = {tup[0]:i for i, tup in list(enumerate(a))[::-1]}
[a[i] for i in sorted(ixs.values())]
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • Why do you need `enumerate` and `[::-1]` - why not just `reversed(a)` and use `None` as a key for all (it's not required for anything useful). Anyway - this won't end up with the same order output as shown by the OPs example... – Jon Clements Jul 02 '13 at 18:19