1

I have an array as below.

arr = np.array(['horse', 'horse', 'horse', 'deer', 'deer', 'horse', 'cat', 'deer'])

Need the indexes only if the adjacent values in the array is not duplicated using python. That is, the expected output is [0,3,5,6,7]. Please help to get this result without for-loop.

2 Answers 2

3

It's a little bit more involved with strings inside an np.array

import numpy as np

arr = np.array(['horse', 'horse', 'horse', 'deer', 'deer', 'horse', 'cat', 'deer'])

factors, indices = np.unique(arr, return_inverse=True)
np.flatnonzero(np.r_[1,np.diff(indices)])

Out:

array([0, 3, 5, 6, 7])
Sign up to request clarification or add additional context in comments.

Comments

2

It doesn't give you the exact answer directly, but I think it is easier to understand. You can compare each value to the one before it to see whether they are equal.

import numpy as np

arr = np.array(['horse', 'horse', 'horse', 'deer', 'deer', 'horse', 'cat', 'deer'])

print( arr[1:] )
# ['horse' 'horse' 'deer' 'deer' 'horse' 'cat' 'deer']
print( arr[:-1] )
# ['horse' 'horse' 'horse' 'deer' 'deer' 'horse' 'cat']
print( arr[1:] != arr[:-1] )
# [False False  True False  True  True  True]

and you can get the indexes by using nonzero().

out = (arr[1:] != arr[:-1]).nonzero()[0] + 1
print( out )
# [3 5 6 7]

To get the exact answer, you can insert the 0 to the start as it is always going to be there.

answer = np.insert(out,0,0)
print( answer )
# [0 3 5 6 7]

Comments

Your Answer

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.