且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从列表中删除重复的和原始的

更新时间:2023-12-04 22:15:40

使用 collections.Counter()对象,然后仅保留计数为1的那些值:

Use a collections.Counter() object, then keep only those values with a count of 1:

from collections import counter

[k for k, v in Counter(lst).items() if v == 1]

这是O(N)算法;您只需要遍历N个项目的列表一次,然后在较少项目(< N)上进行第二次循环以提取仅出现一次的值。

This is a O(N) algorithm; you just need to loop through the list of N items once, then a second loop over fewer items (< N) to extract those values that appear just once.

如果顺序很重要,您正在使用Python< 3.6,将步骤分开:

If order is important and you are using Python < 3.6, separate the steps:

counts = Counter(lst)
[k for k in lst if counts[k] == 1]

演示:

>>> from collections import Counter
>>> lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
>>> [k for k, v in Counter(lst).items() if v == 1]
['a', 'b', 'd']
>>> counts = Counter(lst)
>>> [k for k in lst if counts[k] == 1]
['a', 'b', 'd']

两种方法的顺序相同是一个巧合;对于Python 3.6之前的Python版本,其他输入可能会导致顺序不同。

That the order is the same for both approaches is a coincidence; for Python versions before Python 3.6, other inputs may result in a different order.

在Python 3.6中,字典的实现已更改,并且输入顺序现在得以保留。

In Python 3.6 the implementation for dictionaries changed and input order is now retained.