Removing Duplicates from Nested List Based on First 2 Elements

后端 未结 3 1004
一整个雨季
一整个雨季 2020-12-19 12:11

I\'m trying to remove duplicates from a nested list only if the first 2 elements are the same, ignoring the third...

List:

L = [[\'el1\',\'el2\',\'va         


        
相关标签:
3条回答
  • 2020-12-19 12:23

    If order matters, use a set with only the first two elements of your nested lists:

    seen = set()
    seen_add = seen.add
    return [x for x in seq if tuple(x[:2]) not in seen and not seen_add(tuple(x[:2]))]
    

    or you could use a collections.OrderedDict() object to keep the order; keep the x[:2] slices as keys (as tuples), and extract the values:

    from collections import OrderedDict(
    
    return OrderedDict((tuple(x[:2]), x) for x in seq).values()
    

    In Python 3.6 and up, the standard dict type happens to retain insertion order too:

    return list({tuple(x[:2]): x for x in seq}.values())
    

    The list() call is needed to convert the dictionary view object to a list.

    0 讨论(0)
  • 2020-12-19 12:29

    If the order doesn't matter, you can use that same method but using a tuple of the first and second elements as the key:

    dict(((x[0], x[1]), x) for x in L).values()
    

    Or on Python 2.7 and higher:

    {(x[0], x[1]): x for x in L}.values()
    

    Instead of (x[0], x[1]) you can use tuple(x[:2]), use whichever you find more readable.

    0 讨论(0)
  • 2020-12-19 12:31

    this should do it:

    In [55]: dict((tuple(x[:2]), x) for x in L).values()
    Out[55]: [['el1', 'el2', 'value2'], ['el1', 'el5', 'value3'], ['el3', 'el4', 'value2']]
    
    0 讨论(0)
提交回复
热议问题