Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

python - Remove duplicates and original from list

Given a list of strings I want to remove the duplicates and original word.

For example:

lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']

The output should have the duplicates removed, so something like this ['a', 'b', 'd']

I do not need to preserve the order.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

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.

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]

Demo:

>>> 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']

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...