1

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

Community
  • 1
  • 1
Mntfr
  • 483
  • 6
  • 20

1 Answers1

5

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)]
MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • This approach will only work with pairs, no? `(0, 1, 3)` and `(1, 0, 3)` are not palindromes, but they will be the same set. – juanpa.arrivillaga Feb 02 '17 at 00:39
  • 1
    @juanpa.arrivillaga The [OP said](http://stackoverflow.com/questions/41992343/in-python-how-to-eliminate-repited-elements-and-palindromes-in-a-set-of-lists/41992410#comment71161827_41992343) that it's only about 2-tuples – MSeifert Feb 02 '17 at 00:40
  • @Prune check the original solution before the edits. – juanpa.arrivillaga Feb 02 '17 at 00:52
  • It's still there (first solution), I only added a more general alternative as well (second solution). :) – MSeifert Feb 02 '17 at 00:53