0

I need to find the position of an index based on the existence of an value in a numerical array. I understand that I can use the .index(value) method for list but the thing is I don't want the code to exit or given None or any other type of exception if the value is not present in the array. Instead I would like to know the adjacent array indices values if the exact value is not present in the list. Hope the below example makes the problem statement clear.

arr = [10,20,30,40,50]
Input: 25
Output: 1,2 

Input: 20
Output: 1

Basically the code returns the indices of the adjacent values in the array within which the search input is looked for.

s73
  • 51
  • 8
  • So we can assume that the array is always sorted in ascending order? – Aran-Fey Aug 16 '18 at 20:47
  • Thanks for bringing that up. Yes its always sorted but actually I would prefer to get a solution assuming the array is not sorted too. – s73 Aug 16 '18 at 20:48
  • 1
    If the array is not sorted, what would your expected output be? For example with `Input = 15` and `arr = [10, 20, 14]`, would you expect `0,1` or `1,2` as output? – Aran-Fey Aug 16 '18 at 20:50
  • the answer I am expecting is 1,2 as the test case which I am dealing now is always sorted array. But yeah for completeness if my array is not sorted then I would like to sort it first and then find the indices – s73 Aug 16 '18 at 20:52

1 Answers1

0

For a non-numpy method, you can append your input variable to your list, sort it, and find the index where your input is found in the new list, and return that as well as the previous index:

arr = [10,20,30,40,50]

i = 25

i_index = sorted(arr+[i]).index(i)

>>> [i_index-1, i_index]
[1, 2]

Edit Based on your comment: if the value is already present in the array then I need just the exact index position of that value else I need the adjacent index values within which the value is falling into

You can wrap it in an if statement to check if your input is in your array already:

if i in arr:
    idx = sorted(arr).index(i)
else:
    i_index = sorted(arr+[i]).index(i)
    idx = [i_index-1, i_index]

>>> idx
[1, 2]
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • That won't work correctly if the input array already contains the exact number though. – Aran-Fey Aug 16 '18 at 20:53
  • Wouldn't it? `.index` returns the first index that matches, so if the input is already in the array, it will return the first place in the list where it is found. For instance, given `arr = [10,20,30,40,50, 25]` and `i=25`, it still matches `[1,2]` – sacuL Aug 16 '18 at 20:55
  • Well, if the array contains the exact number then you should only return a single index I think. Your code always returns two indices. – Aran-Fey Aug 16 '18 at 20:57
  • from OP's post: *Basically the code returns the indices of the **adjacent** values in the array within which the search input is looked for.* (my emphasis, as I understand it, they *want* 2 indices) – sacuL Aug 16 '18 at 20:57
  • Yes, but the sentence right before that ends with *"if the value is not present in the array."* – Aran-Fey Aug 16 '18 at 20:58
  • yeah... a bit unclear what they want if the index is already present. Maybe @Sreeder can weight in on this (happy to delete this if it's not a correct solution) – sacuL Aug 16 '18 at 21:00
  • If the value is already present in the array then I need just the exact index position of that value else I need the adjacent index values within which the value is falling into. I have also updated my original question to make this explicit. – s73 Aug 16 '18 at 21:04
  • 1
    actually I tested the code for few options at my end and it looks good for my need now. Thanks a lot for the help, I will use this logic for my code. – s73 Aug 16 '18 at 21:10