Velocity Reviews > returning index of minimum in a list of lists

# returning index of minimum in a list of lists

JJLaRocque@gmail.com
Guest
Posts: n/a

 06-21-2006
Hi all,
Is there a simple python function to return the list index of the
minimum entry in a list of lists?
ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
Or, same question but just for a list of numbers, not a list of lists.
Thanks,
Josh

tactics40@gmail.com
Guest
Posts: n/a

 06-21-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
> Is there a simple python function to return the list index of the
> minimum entry in a list of lists?
> ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
> Or, same question but just for a list of numbers, not a list of lists.
> Thanks,
> Josh

In your example, you returned 2, 4. Did you mean 1, 3?

mylist[1][3] is the way you would access the "1" in your list of lists.

I don't think this task would have a built in function, but you could
write one in less than 4 lines of code easily.

Maric Michaud
Guest
Posts: n/a

 06-21-2006
Le Mercredi 21 Juin 2006 16:54, (E-Mail Removed) a écrit*:
> Hi all,
> Is there a simple python function to return the list index of the
> minimum entry in a list of lists?
> ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
> Or, same question but just for a list of numbers, not a list of lists.
> Thanks,
> Josh

In [7]: min([3, 3, 1, 3])
Out[7]: 1

In [8]: min(min(e) for e in [ [3, 3], [3, 3, 1, 3], [3, 3, 3] ])
Out[8]: 1

regards,

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097

forman.simon@gmail.com
Guest
Posts: n/a

 06-21-2006
(E-Mail Removed) wrote:
> Hi all,
> Is there a simple python function to return the list index of the
> minimum entry in a list of lists?
> ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
> Or, same question but just for a list of numbers, not a list of lists.
> Thanks,
> Josh

One way to do this is to generate (value, index-in-main-list,
index-in-secondary-list) tuples and then just take the minimum.

def f(L):
'''Return indices of the first minimum value in a list of lists.'''
return min(
(n, i, j)
for i, L2 in enumerate(L)
for j, n in enumerate(L2)
)[1:]

L = [[3, 3, 3, 3], [3, 3, 3, 1], [3, 3, 3, 3]]

print f(L) # prints (1, 3)

Note: In python (and most other languages) indices begin at 0, so your
return values of (2, 4) wouldn't be correct.

For a list of numbers it's simpler.

L = [3, 3, 3, 1, 3, 3]
print min((n, i) for i, n in enumerate(L))[1] # prints 3

Hope this helps

~Simon

Paul McGuire
Guest
Posts: n/a

 06-21-2006
"Maric Michaud" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Le Mercredi 21 Juin 2006 16:54, (E-Mail Removed) a écrit :
> Hi all,
> Is there a simple python function to return the list index of the
> minimum entry in a list of lists?
> ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
> Or, same question but just for a list of numbers, not a list of lists.
> Thanks,
> Josh

In [7]: min([3, 3, 1, 3])
Out[7]: 1

In [8]: min(min(e) for e in [ [3, 3], [3, 3, 1, 3], [3, 3, 3] ])
Out[8]: 1

regards,

Read the original posting again. The OP does not want the minimum *value*,
but the *index* of the minimum value.

-- Paul

data = [[3,3,3,3], [3,3,3,1], [3,3,3,3]]

def getMinAndIndex(lst):
minval,minidx = lst[0],0
for i,v in enumerate(lst[1:]):
if v < minval:
minval,minidx = v,i+1
return minval,minidx

subMins = [ getMinAndIndex(sub) for sub in data ]
subMin,subIdx = getMinAndIndex( [s[0] for s in subMins ] )

print "min = %d at [%d][%d]" % (subMin, subIdx, subMins[subIdx][1])

Gives:
min = 1 at [1][3]

JJLaRocque@gmail.com
Guest
Posts: n/a

 06-21-2006
Thanks so much for your help. I was wondering if there was anything
even simpler, but this will be great.

(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > Hi all,
> > Is there a simple python function to return the list index of the
> > minimum entry in a list of lists?
> > ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
> > Or, same question but just for a list of numbers, not a list of lists.
> > Thanks,
> > Josh

>
> One way to do this is to generate (value, index-in-main-list,
> index-in-secondary-list) tuples and then just take the minimum.
>
> def f(L):
> '''Return indices of the first minimum value in a list of lists.'''
> return min(
> (n, i, j)
> for i, L2 in enumerate(L)
> for j, n in enumerate(L2)
> )[1:]
>
> L = [[3, 3, 3, 3], [3, 3, 3, 1], [3, 3, 3, 3]]
>
> print f(L) # prints (1, 3)
>
> Note: In python (and most other languages) indices begin at 0, so your
> return values of (2, 4) wouldn't be correct.
>
> For a list of numbers it's simpler.
>
> L = [3, 3, 3, 1, 3, 3]
> print min((n, i) for i, n in enumerate(L))[1] # prints 3
>
> Hope this helps
>
> ~Simon

Steven Bethard
Guest
Posts: n/a

 06-21-2006
(E-Mail Removed) wrote:
> Is there a simple python function to return the list index of the
> minimum entry in a list of lists?
> ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
> Or, same question but just for a list of numbers, not a list of lists.

In Python 2.5:

Python 2.5a2 (trunk:46491M, May 27 2006, 14:43:55) [MSC v.1310 32 bit
(Intel)] on win32
>>> x = [4, 4, 4, 1]
>>> min(xrange(len(x)), key=x.__getitem__)

3
>>> y = [[3,3,3,3], [3,3,3,1], [3,3,3,3]]
>>> min(xrange(len(y)), key=[min(z) for z in y].__getitem__)

1
>>> def multimin(listoflists):

.... mins = []
.... min_indices = []
.... for sublist in listoflists:
.... min_index = min(xrange(len(sublist)),
.... key=sublist.__getitem__)
.... min_indices.append(min_index)
.... mins.append(sublist[min_index])
.... min_index = min(xrange(len(listoflists)), key=mins.__getitem__)
.... return min_index, min_indices[min_index]
....
>>> multimin([[3,3,3,3], [3,3,3,1], [3,3,3,3]])

(1, 3)

STeVe

jwelby
Guest
Posts: n/a

 06-21-2006
def minIndexFinder(seq):
mins = []
listIndex = 0
result = []
for item in seq:
mins.append([listIndex,min(item),item.index(min(item))])
listIndex += 1
lowest = min([x[1] for x in mins])
for item in mins:
if item[1] == lowest:
result.append([item[0], item[2]])
return result

A bit more verbose, but maybe slightly more readable??

I probably should have used enumerate like Paul did.

For the index of the *first* (or only) occurence of the minimum value
in a list of numbers you can just use:

seq.index(min(seq))

Steven Bethard
Guest
Posts: n/a

 06-21-2006
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>> Is there a simple python function to return the list index of the
>> minimum entry in a list of lists?
>> ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
>> Or, same question but just for a list of numbers, not a list of lists.

>
> One way to do this is to generate (value, index-in-main-list,
> index-in-secondary-list) tuples and then just take the minimum.
>
> def f(L):
> '''Return indices of the first minimum value in a list of lists.'''
> return min(
> (n, i, j)
> for i, L2 in enumerate(L)
> for j, n in enumerate(L2)
> )[1:]
>
> L = [[3, 3, 3, 3], [3, 3, 3, 1], [3, 3, 3, 3]]
>
> print f(L) # prints (1, 3)

I think this is probably the nicest solution. Probably doesn't matter,
but it may be worth noting that if you have more than one minimum value,
this will return the one with the lowest indices (where indices are
ordered lexicographically)::

>>> L = [[3, 2, 1], [1, 2, 3], [2, 1, 3]]
>>> min((n, i, j)

.... for i, L2 in enumerate(L)
.... for j, n in enumerate(L2))[1:]
(0, 2)

STeVe

Bas
Guest
Posts: n/a

 06-21-2006
(E-Mail Removed) wrote:
> Thanks so much for your help. I was wondering if there was anything
> even simpler, but this will be great.

>>> from numpy import *
>>> a=array([[3,3,3,3], [3,3,3,1], [3,3,3,3]])
>>> where(a==a.min())

(array([1]), array([3]))

Probably overkill for your simple problem, but this is a nice
alternative if you do a lot of matrix work.

Bas