Mntfr Mntfr - 4 years ago 99
Python Question

How to eliminate repeated elements and palindromes in a set of lists?

Lets say I have:

a = [(0, 1), (0, 2), (3, 0)]
b = [(4, 5), (2, 0)]
c = [(2, 6), (5,3)]
lists = [a, b, c]


so I would need a function to generate

list = [(0, 1), (0, 2), (3, 0), (4, 5),(2, 6), (5,3) ]


referring to this question I am already able to delete repeated elements but I cant figure out how to solve the palindromes

Answer Source

You can use something like this. I used frozenset because it allows to be hashed and like set it doesn't care about the order - so takes care of your palindromes and duplicates:

from iteration_utilities import unique_everseen
from itertools import chain

a = [(0, 1), (0, 2), (3, 0)]
b = [(4, 5), (2, 0)]
c = [(2, 6), (5,3)]
lists = [a, b, c]

A sample run:

>>> list(unique_everseen(chain.from_iterable(lists), key=frozenset))
[(0, 1), (0, 2), (3, 0), (4, 5), (2, 6), (5, 3)]

The recipe for unique_everseen can also be borrowed from the itertools python documentation page if you don't want an external module.


And if you have items with more than 2 elements you could use this as unique_everseen-function. (slightly changed from the recipe):

def remove_duplicates_and_reversed(iterable):
    seen = set()
    for item in iterable:     
        if item not in seen:
            seen.add(item)       # takes care of duplicates
            seen.add(item[::-1]) # takes care of reversed duplicates
            yield item

>>> list(remove_duplicates_and_reversed(chain.from_iterable(lists)))
[(0, 1), (0, 2), (3, 0), (4, 5), (2, 6), (5, 3)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download