473,385 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

returning index of minimum in a list of lists

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

Jun 21 '06 #1
11 8730

JJ********@gmail.com 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.

Jun 21 '06 #2
Le Mercredi 21 Juin 2006 16:54, JJ********@gmail.com 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
Jun 21 '06 #3
JJ********@gmail.com 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

Jun 21 '06 #4
"Maric Michaud" <ma***@aristote.info> wrote in message
news:ma***************************************@pyt hon.org...
Le Mercredi 21 Juin 2006 16:54, JJ********@gmail.com 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]
Jun 21 '06 #5
Thanks so much for your help. I was wondering if there was anything
even simpler, but this will be great.

fo**********@gmail.com wrote:
JJ********@gmail.com 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


Jun 21 '06 #6
JJ********@gmail.com 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
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
Jun 21 '06 #7
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))

Jun 21 '06 #8
fo**********@gmail.com wrote:
JJ********@gmail.com 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
Jun 21 '06 #9
Bas
JJ********@gmail.com 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

Jun 21 '06 #10
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

Jun 21 '06 #11
JJ********@gmail.com 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


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]

Jun 22 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: techiepundit | last post by:
I'm a Python newbie who just started learning the language a few weeks ago. So these are beginner questions. I have a list of sockets that I use for select.select calls like this: ...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
9
by: Hank Stalica | last post by:
So I've made a linked list class. I have a function that initiates this class, opens a file, parses it, and then inserts nodes into this temporary class. What I would like to do is then have...
12
by: hiro | last post by:
Hi there, I have a 2 lists.. for simplicities sake lets say the are: l1 = l2 = what I need to do is compare l1 against l2 and return the "position" of where each object in l1 is in l2 ...
6
by: xkenneth | last post by:
Looking to do something similair. I'm working with alot of timestamps and if they're within a couple seconds I need them to be indexed and removed from a list. Is there any possible way to index...
0
by: Kam-Hung Soh | last post by:
On Thu, 17 Apr 2008 12:25:51 +1000, Daniel Fetchinson <fetchinson@googlemail.comwrote: See also Section 4.5. Filtering Lists. List comprehension: == 'somestring']
7
by: Benjamin Goudey | last post by:
I have a very large list of integers representing data needed for a histogram that I'm going to plot using pylab. However, most of these values (85%-95%) are zero and I would like to remove them to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.