D_Wills D_Wills - 1 year ago 81
Python Question

how to get the maximum value from a specific portion of a array in python

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

Answer Source

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 searchArray.index(max(slicedArray))

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download