Efficiently remove duplicates, order-agnostic, from list of lists

后端 未结 4 997
感情败类
感情败类 2020-12-21 00:16

The following list has some duplicated sublists, with elements in different order:

l1 = [
    [\'The\', \'quick\', \'brown\', \'fox\'],
    [\'hi\', \'there\'         


        
4条回答
  •  遥遥无期
    2020-12-21 00:37

    This one is a little tricky. You want to key a dict off of frozen counters, but counters are not hashable in Python. For a small degradation in the asymptotic complexity, you could use sorted tuples as a substitute for frozen counters:

    seen = set()
    result = []
    for x in l1:
        key = tuple(sorted(x))
        if key not in seen:
            result.append(x)
            seen.add(key)
    

    The same idea in a one-liner would look like this:

    [*{tuple(sorted(k)): k for k in reversed(l1)}.values()][::-1]
    

提交回复
热议问题