Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > returning index of minimum in a list of lists

Reply
Thread Tools

returning index of minimum in a list of lists

 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      06-21-2006
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

 
Reply With Quote
 
 
 
 
jantod@gmail.com
Guest
Posts: n/a
 
      06-22-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


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]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
locate items in matrix (index of lists of lists) Alexzive Python 6 03-20-2009 06:14 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
List Util insert_at(index, value), delete_at(index) ngoc Perl Misc 5 05-11-2006 12:31 PM
list.index() like...but returning lists (for avoiding '0' onmultiples hits) Srinath Avadhanula Python 7 12-23-2003 03:27 PM



Advertisments