1

I got a fairly large array of arrays of length 2 (List[List[int, int]]) How can I unique arrays of them? Preferably without using different libraries

I've seen several solutions that use numpy, but I'm unlikely to be able to use this in olympiads

# Example input:
nums = [[2, 9], [3, 6], [9, 2], [6, 3]]

for i in nums:
    # some code here

# Output:
# nums = [[2, 9], [3, 6]]

I tried doing this but I guess it's not a very fast solution

# Example input:
nums = [[2, 9], [3, 6], [9, 2], [6, 3]]

unique = []
for i in nums:
    if sorted(i) not in unique:
        unique.append(sorted(i))

# Output:
print(unique) # [[2, 9], [3, 6]]

5
  • 1
    Are builtin libraries allowed? Commented Jan 28, 2023 at 13:26
  • Of course, there is no limit on built-in libraries Commented Jan 28, 2023 at 13:28
  • It seems that by 'unique' you mean 'order independant'; if so, turn your lists into sets (then back to lists if you want) Commented Jan 28, 2023 at 13:29
  • Is the order of elements in the outer list important or can the result be reordered (makes use of sets easier)? Commented Jan 28, 2023 at 13:30
  • Order doesn't important Commented Jan 28, 2023 at 13:34

3 Answers 3

3

To deal with sets not being hashable, you can create a set of frozensets this way:

unique = {frozenset(i) for i in nums}

Then you can use whichever means to turn the results into the objects you want; for example:

unique = [list(i) for i in unique]
Sign up to request clarification or add additional context in comments.

Comments

1

Turn each pair into a sorted tuple, put them all into a set, then turn that back into a list of lists.

>>> nums = [[2, 9], [3, 6], [9, 2], [6, 3]]
>>> {tuple(sorted(n)) for n in nums}
{(2, 9), (3, 6)}
>>> [list(t) for t in {tuple(sorted(n)) for n in nums}]
[[2, 9], [3, 6]]

The tuple is necessary because a set (which is created via the {} set comprehension expression) needs to contain hashable (immutable) objects.

Comments

0

This answer is wrong. The set constructor does not sort its elements.

As @Swifty mentioned, you can use set to solve this problem. Send each pair through the set constructor to sort the pair, then convert it to tuple to make it hashable and use set again to remove duplicate tuples.

nums = [[2, 9], [3, 6], [9, 2], [6, 3]]

num_tuples = set(tuple(set(pair)) for pair in nums)

print(num_tuples)  # {(9, 2), (3, 6)}

Warning: As pointed out by @Samwise

This is a little dodgy because you're assuming that tuple(set(pair)) will deterministically create the same tuple ordering for any given set. This is probably true in practice (IIRC when you iterate over a set the items always come out in hash order, at least in CPython 3) but I'm not sure it's necessarily guaranteed by the Python spec. –

3 Comments

This is a little dodgy because you're assuming that tuple(set(pair)) will deterministically create the same tuple ordering for any given set. This is probably true in practice (IIRC when you iterate over a set the items always come out in hash order, at least in CPython 3) but I'm not sure it's necessarily guaranteed by the Python spec.
@Samwise Thanks for pointing it out. I'll add this as a warning to my answer.
@Samwise Nah, what they're really assuming is that two equal sets become equal tuples. Or in their words: "Send each pair through the set constructor to sort the pair" (emphasis mine). No idea why they're not using sorted for sorting. And their assumption is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.