3

I have a List which contains equal-length-sequences (In this example the length of the sequences is 2, but it can be longer than that in other cases) of numbers, for example:

[[3, 2], [0, 6], [6, 0], [7, 7], [0, 5], [7, 7]]

Now I want to remove all duplicates ([7, 7] appears twice) but also remove all reverse duplicates ([0, 6] and [6, 0]).

My current code is this:

def getUniqueShapes(shapes):
    unique = []
    for shape in shapes:
        if shape not in unique and reversed(shape) not in unique:
            shapes.append(shape)

    print(unique)

My expected output for this example is

[[3, 2], [0, 6], [7, 7], [0, 5]]

Because of the not in, this algorithm runs with very poor speed. Is there an easy way I can apply such a condition to the list with a better time complexity?

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
Ian Rehwinkel
  • 2,486
  • 5
  • 22
  • 56

1 Answers1

4
  • Assuming in case of [1, 0], [0, 1] both means same.
  • You need to convert lists to tuple first because list is not hashable.
  • Also output is set of tuples you can easily convert it to list of lists.
>>> l = [[1, 2], [2, 1], [3, 4], [5, 6], [5, 6]]
>>> set(map(tuple, map(sorted, l)))
{(1, 2), (3, 4), (5, 6)}

Edit: This will work for any length cases elements.

Engineero
  • 12,340
  • 5
  • 53
  • 75
Poojan
  • 3,366
  • 2
  • 17
  • 33