4

I have a specific scenario that I need to scan a specific portion of an array for a maximum value of that portion and return the position of that value with regards to the entire array. for example

 searchArray = [10,20,30,40,50,60,100,80,90,110]

I want to scan for the max value in portion 3 to 8, (40,50,60,100,80,90)

and then return the location of that value.

so in this case max value is 100 and location is 6

is there a way to get that using python alone or with help oy numpy

1
  • position = searchArray.index(max(ary[3:8])) here 3 is the lower bound and 8 is the upper bound Commented May 25, 2016 at 8:01

6 Answers 6

2

First slice your list and then use index on the max function:

searchArray = [10,20,30,40,50,60,100,80,90,110]
slicedArray = searchArray[3:9]
print slicedArray.index(max(slicedArray))+3

This returns the index of the sliced array, plus the added beginSlice

Sign up to request clarification or add additional context in comments.

7 Comments

This doesn't provide the correct index value. It should be the index within the original list
I said that in the last line, but I changed the code now
No problem! If you could accept the answer that would be perfect
but what about print ` print searchArray.index(max(slicedArray)) . this works fine as well isnt it
First calling max which iterates through the whole list to find the highest value and then calling index which iterates through half the list again to find where that element was is redundant and should be avoided if performance matters. Check out my answer using an enumerate generator to get the indexes before searching the max value and avoid an unnecessary second iteration over the list.
|
1

Try this...Assuming you want the index of the max in the whole list -

import numpy as np

searchArray = [10,20,30,40,50,60,100,80,90,110]

start_index = 3
end_index = 8

print (np.argmax(searchArray[start_index:end_index+1]) + start_index)

2 Comments

this is the only acceptable answer to this question
@EelcoHoogendoorn Why would you think so? Numpy is not even notably faster on really huge lists, so what is the advantage of this solution over the others in your opinion? Please explain.
1

Use enumerate to get an enumerated list of tuples (actually it's a generator, which means that it always only needs memory for one single entry and not for the whole list) holding the indexes and values, then use max with a custom comparator function to find the greatest value:

searchArray = [10,20,30,40,50,60,100,80,90,110]
lower_bound = 3  # the lower bound is inclusive, i.e. element 3 is the first checked one
upper_bound = 9  # the upper bound is exclusive, i.e. element 8 (9-1) is the last checked one

max_index, max_value = max(enumerate(searchArray[lower_bound:upper_bound], lower_bound),
                           key=lambda x: x[1])

print max_index, max_value
# output: 6 100

See this code running on ideone.com

7 Comments

Thanks a lot. but using slicer is effective on this case i think
@D_Wills This approach uses slicing as well ([lower_bound:upper_bound]) - it just avoids searching the list again for the item returned by max just to find out its index. Instead I generate tuples of index and value on the fly using enumerate and let max process them. That way it only performs one iteration and directly returns both the index and the value.
I think you mean max and greatest value.
I like the enumerate(..., lower_bound) as well as other subtle issues... best answer
@ByteCommander thanks a lot for that. but when u put the upper bound = 9, then it should scan position 9 as well. this code does not scan the last position. then the answer should be position 9 value 110
|
0

I'd do it like this:

sliced = searchArray[3:9]
m = max(sliced)
pos = sliced.index(m) + 3

I've added an offset of 3 to the position to give you the true index in the unmodified list.

Comments

0

With itemgetter:

pos = max(enumerate(searcharray[3:9], 3), key=itemgetter(1))[0]

Comments

-3

i guess this what you want

maxVal = max(searchArray[3:8]) // to get max element

position = searchArray.index(max(ary[3:8])) //to get the position of the index

1 Comment

Doesn't answer the question, they want the index

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.