Re: returning index of minimum in a list of lists
[EMAIL PROTECTED] 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] -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
[EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
Le Mercredi 21 Juin 2006 16:54, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
Thanks so much for your help. I was wondering if there was anything even simpler, but this will be great. [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
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)) -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
[EMAIL PROTECTED] wrote: [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: returning index of minimum in a list of lists
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 -- http://mail.python.org/mailman/listinfo/python-list