Re: Is there a simple way to find the list index to the max value?
On 2010-02-16 11:44:45 -0800, a...@pythoncraft.com (Aahz) said: In article <4b7a91b1.6030...@lonetwin.net>, steve wrote: On 02/16/2010 05:49 PM, W. eWatson wrote: 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). I don't think you understand order notation. There's no such thing as O(2N). To answer the original question, how about: max(enumerate(l), key=lambda x: x[1])[0] As to whether this is faster than index(max()), you'd have to time it. -Tom -- http://mail.python.org/mailman/listinfo/python-list
Is there a simple way to find the list index to the max value?
See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
a...@pythoncraft.com (Aahz) writes: > In article <4b7a91b1.6030...@lonetwin.net>, steve wrote: >>On 02/16/2010 05:49 PM, W. eWatson wrote: >>> >>> 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). :-) Joke aside, even though you traverse the list twice, it may still be quicker than other solutions because both max and list.index are C functions and no intermediate object is constructed. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
On 16/02/2010 19:44, Aahz wrote: In article<4b7a91b1.6030...@lonetwin.net>, steve wrote: On 02/16/2010 05:49 PM, W. eWatson wrote: 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). Well you could do this: a.sort () return -1 TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
In article <4b7a91b1.6030...@lonetwin.net>, steve wrote: >On 02/16/2010 05:49 PM, W. eWatson wrote: >> >> 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). -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
On 2/16/2010 4:41 AM, Arnaud Delobelle wrote: Arnaud Delobelle writes: "W. eWatson" writes: 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: a = [1,4,9,3] max_index = a.index(max(a)) max_index 2 Ah, the good one for last! Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
Arnaud Delobelle writes: > "W. eWatson" writes: > >> 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: >>> a = [1,4,9,3] >>> max_index = a.index(max(a)) >>> max_index 2 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
On 02/16/2010 05:49 PM, W. eWatson wrote: 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 ? cheers, - steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a simple way to find the list index to the max value?
"W. eWatson" writes: > See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2. Here are a few ways. >>> a = [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 2 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list