Get unique elements from list of lists when the order of the sublists does not matter

时间秒杀一切 提交于 2020-04-01 02:00:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!