0

I have the following list:

x = np.array([1, 1, 2, 2, 2])

with np.unique values of [1, 2]

How do I generate the following list:

[1, 2, 1, 2, 3]

i.e. a running index from 1 for each of the unique elements in the list x.

asked Jul 16, 2019 at 10:39
0

3 Answers 3

2

you can use pandas.cumcount() after grouping by the value itself, it does exactly that:

Number each item in each group from 0 to the length of that group - 1.

try this:

import numpy as np
import pandas as pd
x = np.array([1, 1, 2, 2, 2])
places = list(pd.Series(x).groupby(by=x).cumcount().values + 1)
print(places)

Output:

[1, 2, 1, 2, 3]
answered Jul 16, 2019 at 10:49
2
  • Thanks. Is there a way to do this with out pandas? Commented Jul 16, 2019 at 10:52
  • 1
    @alex_lewis I tried with native numpy function but couldn't get any nice solution. the other way I think of is using list.index in a loop, which will be 100x times slower :( maybe someon else will come up with something numpy only. Commented Jul 16, 2019 at 11:07
2

Just use return_counts=True of np.unique with listcomp and np.hstack. It is still faster pandas solution

c = np.unique(x, return_counts=True)[1]
np.hstack([np.arange(item)+1 for item in c])
Out[869]: array([1, 2, 1, 2, 3], dtype=int64)
answered Jul 16, 2019 at 11:14
1
  • this will only work if the values are consecutive. for x = np.array([1, 1, 2, 2, 2, 1]) you'll get [1 2 3 1 2 3] while you should be getting [1 2 1 2 3 3] Commented Jul 16, 2019 at 12:01
1

I'm not sure, if this is any faster or slower solution, but if you need just a list result with no pandas, you could try this

arr = np.array([1, 1, 2, 2, 2])
from collections import Counter
ranges = [range(1,v+1) for k,v in Counter(arr).items()]
result = []
for l in ranges:
 result.extend(list(l))
print(result)

[1, 2, 1, 2, 3]

(or make your own counter with dict instead of Counter())

answered Jul 16, 2019 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.