Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 5
    Could you please explain what r_ is? Commented Mar 23, 2011 at 18:55
  • 1
    @Geoff, r_ concatenates; or, more precisely, it translates slice objects to concatenation along each axis. I could have used hstack instead; that may have been less confusing. See the documentation for more information about r_. There is also a c_. Commented Mar 24, 2011 at 19:58
  • +1, nice one! (vs NP.where) your solution is a lot simpler (and probably faster) in the case where it's only the first occurrence of a given value in a 1D array that we need Commented Feb 14, 2014 at 1:33
  • 4
    The latter case (finding the first index of all values) is given by vals, locs = np.unique(t, return_index=True) Commented Nov 2, 2015 at 15:39
  • 1
    This answer assumes that the array is already sorted, which was not a condition provided in the original question. It also assumes that the final number is a duplicate. For example, try [1,2,1,2,1] vs [1,1,1,2,2] and [1,2] vs [1,2,2] respectively. Commented Jun 8, 2022 at 14:30