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

后端 未结 2 1642
Happy的楠姐
Happy的楠姐 2020-12-04 03:25

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]         


        
相关标签:
2条回答
  • 2020-12-04 03:46

    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]]
    
    0 讨论(0)
  • 2020-12-04 04:04

    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)}
    
    0 讨论(0)
提交回复
热议问题