returning index of minimum in a list of lists

Discussion in 'Python' started by JJLaRocque@gmail.com, Jun 21, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    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, 2006
    #2
    1. Advertising

  3. Le Mercredi 21 Juin 2006 16:54, 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
    Maric Michaud, Jun 21, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. Paul McGuire Guest

    "Maric Michaud" <> wrote in message
    news:...
    Le Mercredi 21 Juin 2006 16:54, 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]
    Paul McGuire, Jun 21, 2006
    #5
  6. Guest

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

    wrote:
    > 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, 2006
    #6
  7. 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
    Steven Bethard, Jun 21, 2006
    #7
  8. jwelby Guest

    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))
    jwelby, Jun 21, 2006
    #8
  9. wrote:
    > 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
    Steven Bethard, Jun 21, 2006
    #9
  10. Bas Guest

    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
    Bas, Jun 21, 2006
    #10
  11. Guest

    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 22, 2006
    #11
  12. Guest

    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, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Srinath Avadhanula
    Replies:
    7
    Views:
    288
    Dang Griffith
    Dec 23, 2003
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    391
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Alexzive
    Replies:
    6
    Views:
    595
    Chris Rebert
    Mar 20, 2009
  4. ngoc
    Replies:
    5
    Views:
    168
    Tad McClellan
    May 11, 2006
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    271
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page