Remove partially duplicate tuples from list of tuples

前端 未结 3 826
情书的邮戳
情书的邮戳 2020-12-11 10:31

I have a list of tuples and need to delete tuples if its 1st item is matching with 1st item of other tuples in the list. 3rd item may or may not be the same, so I cannot use

相关标签:
3条回答
  • 2020-12-11 10:59

    You should avoid modifying your list in place while iterating over it. Instead, you can use the popular itertools unique_everseen recipe, also available in 3rd party more_itertools. Just use operator.itemgetter in the key argument:

    from more_itertools import unique_everseen
    from operator import itemgetter
    
    res = list(unique_everseen(L, key=itemgetter(0, 1)))
    

    This solution takes O(n) time, but is generally less efficient than a dictionary-based solution, although it is arguably clearer in its intention.

    0 讨论(0)
  • 2020-12-11 11:04

    The usual way is keying a dict off whatever you want to dedupe by, for example:

    >>> a = [(0, 13, 'order1'), (14, 27, 'order2'), (14, 27, 'order2.1'), (0, 13, 'order1'), (28, 41, 'order3')] 
    >>> print(*{tup[:2]: tup for tup in a}.values()) 
    (0, 13, 'order1') (14, 27, 'order2.1') (28, 41, 'order3')
    

    This is O(n) time complexity, superior to O(n log n) groupby based approaches.

    0 讨论(0)
  • 2020-12-11 11:25

    You can get the first element of each group in a grouped, sorted list:

    from itertools import groupby
    from operator import itemgetter
    
    a = [(0, 13, 'order1'), (14, 27, 'order2'), (14, 27, 'order2.1'), (0, 13, 'order1'), (28, 41, 'order3')]
    
    result = [list(g)[0] for k, g in groupby(sorted(a), key=itemgetter(0))]
    print(result)
    
    0 讨论(0)
提交回复
热议问题