W
W. eWatson
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
W. eWatson said:See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
2a = [1,4,9,3]
max_index = max(xrange(len(a)), key=a.__getitem__)
max_index 2
# Or: .... max_index = max((n, i) for i, n in enumerate(a))[1]
max_index 2
# Or: .... from itertools import *
max_index = max(izip(a, count()))[1]
max_index
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
Arnaud Delobelle said:W. eWatson said:See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
Here are a few ways.
2a = [1,4,9,3]
max_index = a.index(max(a))
max_index
Ah, the good one for last! Thanks.Arnaud Delobelle said:W. eWatson said:See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
Here are a few ways.
[...]
My copy past went wrond and I forgot the first one:
2a = [1,4,9,3]
max_index = a.index(max(a))
max_index
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
The most obvious would be a.index(max(a)). Is that what you wanted ?
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
The most obvious would be a.index(max(a)). Is that what you wanted ?
The disadvantage of that is that it's O(2N) instead of O(N).
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
The most obvious would be a.index(max(a)). Is that what you wanted ?
The disadvantage of that is that it's O(2N) instead of O(N).
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
The most obvious would be a.index(max(a)). Is that what you wanted ?
The disadvantage of that is that it's O(2N) instead of O(N).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.