-1

Is there a python or numpy function that returns the argument or index of some value (or set of values) that exists in an iterable? I want something that is similar to the functionality of:

>>c= np.array([1,2,3])
>>np.argmin(c)
0

but is able to do:

>>c.*somefunction*(2)
1

In the pandas library, you can create a boolean mask that returns only the values that match the condition, and from there you can return the indices of these values. But how might this be done without pandas?

jbplasma
  • 385
  • 4
  • 14
  • Is it possible you're looking for [`filter`](https://docs.python.org/3/library/functions.html#filter)? Otherwise, I'm not sure I understand your question. – Silvio Mayolo Jul 18 '18 at 22:06
  • Your question doesn't make sense. Most iterables *don't have indices*, those that do, frequently provide such a functionality as methods on those objects or, in the case of `numpy`, you can use `numpy.where` – juanpa.arrivillaga Jul 18 '18 at 22:12
  • The answer to the other question only returns the first index. For all the indices, you can do `[index for index,value in enumerate(c) if value == target]`. Note that this will return a list, even if there is only one such value. – Acccumulation Jul 18 '18 at 22:16
  • 1
    Most of the answers in the duplicate are for a list, which has an `index` method. But `c` is a numpy. `np.where(c==2)[0]` a widely used equivalent for arrays. `c==2` is the boolean mask, and where returns a tuple of indices where that is true. – hpaulj Jul 19 '18 at 01:21

1 Answers1

2

You are looking for

list.index(value)
Ishan Srivastava
  • 1,129
  • 1
  • 11
  • 29