returning index of minimum in a list of lists

J

JJLaRocque

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
 
T

tactics40

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.
 
M

Maric Michaud

Le Mercredi 21 Juin 2006 16:54, (e-mail address 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
 
F

forman.simon

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
 
P

Paul McGuire

Le Mercredi 21 Juin 2006 16:54, (e-mail address 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]
 
J

JJLaRocque

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

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
 
S

Steven Bethard

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
Type "help", "copyright", "credits" or "license" for more information.
>>> 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
 
J

jwelby

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))
 
S

Steven Bethard

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
 
B

Bas

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
 
B

bearophileHUGS

This way is probably slowe (two scans of the list for l1, and even more
work for l2), but for small lists it's probably simple enough to be
considered:

For a simple list:
l1 = [5, 3, 2, 1, 4]
l1.index(min(l1))
3


For a list of lists:
l2 = [[3, 3, 3, 3], [6], [10], [3, 3, 3, 1, 4], [3, 0, 3, 3]]
mins = map(min, l2)
mins [3, 6, 10, 1, 0]
pos1 = mins.index(min(mins))
pos1 4
subl = l2[pos1]
subl.index(min(subl))
1

This solution is also fragile:
l3 = [[3], []]
mins = map(min, l3)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
ValueError: min() arg is an empty sequence

Bye,
bearophile
 
J

jantod

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

Untested:

items = []
for x, a in enumerate(L):
for y, b in enumerate(a):
items.append((b, (x,y)))
x, y = min(items)[1]

You could also change this to a generator:

def f(L):
for x, a in enumerate(L):
for y, b in ebumerate(a):
yield b, (x,y)

x, y = min(f(L))[1]
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top