I am able to get the maximum value in the array with an explicit loop:
def main():
a = [2,1,5,234,3,44,7,6,4,5,9,11,12,14,13]
max = 0
for number in a:
if number > max:
max = number
print(max)
if __name__ == '__main__':
main()
How can I get the index (position) of that value?
9 Answers 9
In my code I would use this:
>>> max(enumerate(a),key=lambda x: x[1])[0]
3
2 Comments
max(range(len(a)), key=lambda i: a[i])max(zip(a, range(len(a)))[1] although if two elements are equal, this will return the element with the highest index, while yours will return the element with the lowest index.A simple one liner of:
max( (v, i) for i, v in enumerate(a) )[1]
This avoids having to .index() the list after.
3 Comments
(5,3) < (5,4) returns True1, 3, 3, 3, 5 would compare i 1 < 2 and 2 < 3, although we'd only need to keep track of the current max value's index instead of comparing anything.Update:
max_idx = -1
max_val = a[0]
for i in xrange(1, len(a)):
if a[i] > max_val:
max_val = a[i]
max_idx = i
This doesn't shadow built-in function max(), and also will give correct answers for lists that consist of only negative values.
Previous solution
a.index(max(a))
will do the trick.
Built-in function max(a) will find the maximum value in your list a, and list function
index(v) will find the index of value v in your list. By combining them, you get what you are looking for, in this case the index value 3.
Note that .index() will find the index of the first item in the list that matches, so if you had several identical "max" values, the index returned would be the one for the first.
For more information:
In the spirit of "Simple is better than complex." (Zen of Python)
2 Comments
If you aren't allowed to use the built in index() function, just iterate with an index, instead of using a foreach loop.
for i in range(len(a)):
if a[i] > max:
max = a[i]
maxIndex = i
9 Comments
range and len are builtin functions :)max (which may not be a problem since we aren't suppose to use it .. :-) but probably not good practice, and wouldn't work correctly for lists that only contain negative valuesUse the argmax method of the numpy.array object.
import numpy as np
np.array(a).argmax()
Comments
You can use enumerate to also give you an index while iterating through a list:
>>> a = [2, 1, 5, 234, 3, 44, 7, 6, 4, 5, 9, 11, 12, 14, 13]
>>> maxIndex, maxNumber = 0, 0
>>> for index, number in enumerate(a):
if number > maxNumber:
maxIndex = index
maxNumber = number
>>> maxIndex, maxNumber
(3, 234)
Comments
this is way simpler
x.index(max(x)) #where x is your list
1 Comment
Use the index(x) function. See the documentation here http://docs.python.org/tutorial/datastructures.html
def main():
a = [2,1,5,234,3,44,7,6,4,5,9,11,12,14,13]
max = 0
for number in a:
if number > max:
max = number
max_index = a.index(max)
print max
However, this is not as fast as other suggested answers (e.g. using enumerate). Simple though.
Comments
If you like powerfull code you would like this :) If you just have integer numbers you can substitute float by int.
maximum= max(map(float,[2,1,5,234,3,44,7,6,4,5,9,11,12,14,13]))
If you have your input in a text file do this:
file.txt2 1 5 234 3 44 7 6 4 5 9 11 12 14 13
maximum= max(map(float,(open('file.txt', 'r').readline()).split()))
if number > maxcould be considered using the builtin method__gt__of the built-in typelist:) Seriously though - why can't you just use the built-inmax- just an intellectual exercise or self-torture?maxis the correct, concise and efficient method for this in Python, but you're after ways of how not to do it?