Home > Software design >  Filter unique pair from list of tuples in python
Filter unique pair from list of tuples in python

Time:04-13

I have a list of tuples like [(A,B),(B,A),(D,C),(E,F),(C,D),(F,E)]. I want result as [(A,B),(C,D),(E,F)]?

Example:

[('1.1.1.10', '140.205.94.193'), ('1.1.1.10', '157.240.16.35'), ('1.1.1.10', '172.217.163.110'),('157.240.16.35', '1.1.1.10'),('140.205.94.193', '1.1.1.10')]

Expected result:

[('1.1.1.10', '140.205.94.193'),('1.1.1.10', '157.240.25.35'),('1.1.1.10', '172.217.163.110'),]

CodePudding user response:

This makes a list where there are no duplicate permutations of x

a = [('1.1.1.10', '140.205.94.193'),
     ('1.1.1.10', '157.240.16.35'),
     ('1.1.1.10', '172.217.163.110'),
     ('157.240.16.35', '1.1.1.10'),
     ('140.205.94.193', '1.1.1.10')]


def clean(x):
    heap = []
    for i in x:
        if (i[0], i[1]) not in heap and (i[1], i[0]) not in heap:
            heap.append(i)
    return heap
>>> clean(a)
[('1.1.1.10', '140.205.94.193'),
 ('1.1.1.10', '157.240.16.35'),
 ('1.1.1.10', '172.217.163.110')]

CodePudding user response:

You can utilize set() and sorted() to eliminate duplicated tuple in any order.

raw =[
    ('1.1.1.10', '140.205.94.193'), 
    ('1.1.1.10', '157.240.16.35'), 
    ('1.1.1.10', '172.217.163.110'),
    ('157.240.16.35', '1.1.1.10'),
    ('140.205.94.193', '1.1.1.10')
]

print(list(set([tuple(sorted(r)) for r in raw])))

Result:

[('1.1.1.10', '140.205.94.193'), ('1.1.1.10', '157.240.16.35'), ('1.1.1.10', '172.217.163.110')]

CodePudding user response:

Looks like you have an array of tuples? I believe you could create another array such that arr2[1] = arr[1]. arr[5] = arr2[2]. arr[6] = arr2[3].

That is if you always have the same size list, since it is very small. This may be the shortest way to do it

CodePudding user response:

Here is some witchery that does the job.

(This is a little cryptic, so if I was you, I would do something more explicit, like the options available here: Remove duplicates with a different equality test in Python)

data =  [('1.1.1.10', '140.205.94.193'), ('1.1.1.10', '157.240.16.35'), ('1.1.1.10', '172.217.163.110'),('157.240.16.35', '1.1.1.10'),('140.205.94.193', '1.1.1.10')]

res = [(a, b) for a, b in reversed(data) if (c := data.pop()) and (a, b) not in data and (b, a) not in data]
  • Related