-2
list_val = [ 30, 120, 240, 510]
num = 900

np.argmax(np.asarray(list_val) > num)

I want to find the position of first element in array which is greater than num

The output from code above is 0. However, since 900 is greater than 510, the result should be 3. How can I fix it?

--EDIT

I want a solution that works if num is 20, 200 or 900.

user308827
  • 21,227
  • 87
  • 254
  • 417
  • Simply `np.argmax(np.asarray(list_val))`, I suspect. Otherwise, I don't understand what you are trying to achieve? – MariusSiuram Jul 17 '17 at 15:59
  • So I do not know before hand if num will be 900 or 200 or 20. Is there a solution that works for all cases? – user308827 Jul 17 '17 at 16:01
  • 1
    Can you try to explain in a better/detailed way why it should be 3? – Divakar Jul 17 '17 at 16:02
  • ...for example with a more "interesting" list like `[30, 10000, 500, 899, 901, 10000, 500]` – MariusSiuram Jul 17 '17 at 16:03
  • @Divakar, I want to find first element in array which is greater than `num`. will clarify in question. – user308827 Jul 17 '17 at 16:05
  • 1) `want to find the position of first element in array which is greater than num`. 2) `since 900 (num) is greater than 510, the result should be 3. ` Contradictory statements. Voting to close, on grounds of being not clear. – Divakar Jul 17 '17 at 16:09
  • I think you are looking for `np.where` or `np.flatnonzero`, but this is still not clear. – cs95 Jul 17 '17 at 16:09
  • "Since 900 is greater than 510"… but the equation is "510 > 900", which is false… – Dietrich Epp Jul 17 '17 at 16:10
  • Possible duplicate of [Is there a Numpy function to return the first index of something in an array?](https://stackoverflow.com/questions/432112/is-there-a-numpy-function-to-return-the-first-index-of-something-in-an-array) (according to the last clarification). Or simply not clear as-is now. – MariusSiuram Jul 18 '17 at 07:34

2 Answers2

1
In [248]: x = np.array([ 30, 120, 240, 510])
In [249]: x>200
Out[249]: array([False, False,  True,  True], dtype=bool)
In [250]: np.argmax(_)
Out[250]: 2
In [251]: x>400
Out[251]: array([False, False, False,  True], dtype=bool)
In [252]: np.argmax(_)
Out[252]: 3
In [253]: x>600
Out[253]: array([False, False, False, False], dtype=bool)
In [254]: np.argmax(_)
In [255]: np.max(__)
Out[255]: False

With the large threshhold, the comparison produces all False. The maximum value is then False, and the 0th item is that.

You may have to test the x>n for all False and return a different value in that case. This is not a universally defined behavior.

Lists have a find

In [261]: (x>200).tolist().index(True)
Out[261]: 2
In [262]: (x>400).tolist().index(True)
Out[262]: 3
In [263]: (x>600).tolist().index(True)
...
ValueError: True is not in list

The string find returns a -1 if the value is not found.


In [266]: def foo(test):
     ...:     if not test.any():
     ...:         return -1
     ...:     return np.argmax(test)
     ...: 
In [267]: foo(x>200)
Out[267]: 2
In [268]: foo(x>400)
Out[268]: 3
In [269]: foo(x>600)
Out[269]: -1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

Say a=np.array([ 30, 120, 240, 510]). np.argmax() will gives you 3. Your code evals 0 is because np.asarray(list_val) > num is an array whose entries all False i.e. no element is greater than num, so np.argmax takes False as 0 and throws a zero as argmax.

If you'd like to have a threshold, try np.argmax(a[a<num]). This will raise ValueError instead of 0 if nothing meets the threshold.

Nriuam
  • 101
  • 9