Get Unique Elements From List Of Lists When The Order Of The Sublists Does Not Matter
I have a list of pairs, representing all edges of cluster in a graph. Actually the list is bigger than this. This is just an example. [[1, 2], [2, 1], [3, 5], [6, 3], [3, 6]] [1,
Solution 1:
If order of the pairs' elements is irrelevant, then use sorted
to normalize pairs. Then, use a set
to eliminate duplicates.
lst = [1, 2], [2, 1], [3, 5], [6, 3], [3, 6]
unique_pairs = {tuple(sorted(p)) for p in lst} # {(1, 2), (3, 6), (3, 5)}
Solution 2:
To remove duplicates whilst preserving order, you can key off a dict:
>>> data = [1, 2], [2, 1], [3, 5], [6, 3], [3, 6]
>>> list({frozenset(edge): edge for edge in data}.values())
[[2, 1], [3, 5], [3, 6]]
Order is preserved overall, and also the order within each pair. In case of dupes, the last pair seen will be the one kept in result. You could keep the first pair by reversed iteration:
>>> list({frozenset(edge): edge for edge in reversed(data)}.values())[::-1]
[[1, 2], [3, 5], [6, 3]]
If you have an older version of Python (<3.6) where the standard dict is not order-preserving, then do the same using an OrderedDict
:
>>> from collections import OrderedDict
>>> list(OrderedDict((frozenset(edge), edge) for edge in data).values())
[[2, 1], [3, 5], [3, 6]]
Post a Comment for "Get Unique Elements From List Of Lists When The Order Of The Sublists Does Not Matter"