简体   繁体   中英

Most efficient way to find the indexes of unique values in a Python 3 list

What is the most efficient way to find the indexes of strings in a list that occur only once?

foo = ['it', 'does', 'it', 'very', 'very', 'well']
bar = ???  # bar = [1, 5]

I already know about sets, dictionaries and list comprehensions. The problem I'm trying to solve here is in my production code I have parallel data lists where the index of one is the index of many which can't be changed for historical reasons.

With collections.Counter subclass:

import collections

foo = ['it', 'does', 'it', 'very', 'very', 'well']
counts = collections.Counter(foo)
result = [i for i,v in enumerate(foo) if counts[v] == 1]

print(result)

The output:

[1, 5]

You will get what you want. Dictionaries are faster in python

from collections import Counter
foo = ['it', 'does', 'it', 'very', 'very', 'well']
d = dict(Counter(foo))
[i for i,v in enumerate(foo) if counts[v]  == 1]

You can also use set(foo)

You can try something like this, especially if the size of your foo list is bigger than in your example above and have lots of duplicates.

seen = set()
[i for i,e in enumerate(foo) if not (e in seen or seen.add(e) or e in foo[i+1:])]

It depends on the kind of efficiency you would like to get. You could do this directly in a list comprehension, straightforward and readable:

bar = [index for index,el in enumerate(foo) if foo.count(el)==1]

Please see this for info if you would like to use Counter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM