问题
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, 2] means an edge between nodes 1 and 2, and so does [2, 1].
So, I have some difficulty to eliminate the repeated pairs to count the in-degrees of the cluster. 
The output should be like [[1, 2], [3, 5], [3, 6]] or [[2, 1], [3, 5], [6, 3]]
回答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)}
回答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]]
来源:https://stackoverflow.com/questions/50769558/get-unique-elements-from-list-of-lists-when-the-order-of-the-sublists-does-not-m