I have a list like this:
l=[1,2,2,3,4,5,5,5]
We can see that the list list contains 5 unique values with 8 total values.
I want the indexes of the unique values from the list in list format.
So the output looks like:
indexes=[0,1,3,4,5]
How to do it most efficient way using python ?
why did no one mention np.unique
in here??
import numpy as np
np.unique([1,6,6,2,2,3,4,5,5,5], return_index=True)
>>> (array([1, 2, 3, 4, 5, 6]), array([0, 3, 5, 6, 7, 1], dtype=int64))
the first array contains the ( sorted ) unique values and the second array (returned if return_index
is set to True
) is a list of the indexes of the first occurrences
You can use built-in types for this.
CODE
l=[1,2,2,3,4,5,5,5]
indexes = [l.index(x) for x in set(l)]
EXPLANATION
set
list.index
COMMENT
As pointed out in the comments, if order is important for you, you can either use sorted
on the set
or on the resulting index list depending on the data that is provided. If the data is already sorted, I would suggest to do it like this:
indexes = [l.index(x) for x in sorted(set(l))]
You can just iterate through your list. The first time you see an item add it to a set indicating it's been seen and add the number to the result list. Skip the others. This will keep the indexes in the order the item are first seen in the list:
def uniqueIndexes(l):
seen = set()
res = []
for i, n in enumerate(l):
if n not in seen:
res.append(i)
seen.add(n)
return res
l=[1,2,2,3,4,5,5,5,2]
uniqueIndexes(l)
results:
[0, 1, 3, 4, 5]
If this isn't a pandas-specific question, and l
is just a plain old list, I'd go over it and keep an ordered map from the value to the first index holding it. Then, you can return the map values:
from collections import OrderedDict
def get_unique_indexes(l):
# OrdedDict is used to preserve the order of the indexes
result = OrderedDict()
for i in range(0, len(l)):
val = l[i]
if not val in result:
result[val] = i
return result.values()
This can be done:
l=[1,2,2,3,4,5,5,5] # Your list
indexes=[] # Your output list
for elem in set(l):
indexes.append(l.index(elem))
In the for loop, each element of the set is taken and the index of the element in the list is taken out using list.index() method (which returns the index of the first element of the required type) and the value is inserted into the indexes list.
This is how it works.(I like to explain, so please apologize for my lengthy writing!)
Thank You.
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.