maximum element?

Discussion in 'Python' started by Ivan Voras, Mar 4, 2004.

  1. Ivan Voras

    Ivan Voras Guest

    What is the 'most pythonic' way of searching the largest element in a
    list/tuple?

    My 'standard' idea is:

    max = list[0] # or -infinity, or whatever...
    for i in list:
    if i > max:
    max = i

    While this is ok, I somehow 'feel' there could be a more concise
    solution... :)
     
    Ivan Voras, Mar 4, 2004
    #1
    1. Advertising

  2. At some point, Ivan Voras <ivoras@__geri.cc.fer.hr> wrote:

    > What is the 'most pythonic' way of searching the largest element in a
    > list/tuple?
    >
    > My 'standard' idea is:
    >
    > max = list[0] # or -infinity, or whatever...
    > for i in list:
    > if i > max:
    > max = i
    >
    > While this is ok, I somehow 'feel' there could be a more concise
    > solution... :)


    max(list)

    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)physics(dot)mcmaster(dot)ca
     
    David M. Cooke, Mar 4, 2004
    #2
    1. Advertising

  3. On Thu, 04 Mar 2004 21:52:50 +0100,
    Ivan Voras <ivoras@__geri.cc.fer.hr> wrote:

    > What is the 'most pythonic' way of searching the largest element in a
    > list/tuple?


    > My 'standard' idea is:


    > max = list[0] # or -infinity, or whatever...
    > for i in list:
    > if i > max:
    > max = i


    > While this is ok, I somehow 'feel' there could be a more concise
    > solution... :)


    How about max( list )?

    Regards,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
     
    Heather Coppersmith, Mar 4, 2004
    #3
  4. Ivan Voras

    Robert Kern Guest

    Ivan Voras wrote:
    > What is the 'most pythonic' way of searching the largest element in a
    > list/tuple?


    max(list)

    > My 'standard' idea is:
    >
    > max = list[0] # or -infinity, or whatever...
    > for i in list:
    > if i > max:
    > max = i
    >
    > While this is ok, I somehow 'feel' there could be a more concise
    > solution... :)


    Indeed. :)

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Mar 4, 2004
    #4
  5. Ivan Voras

    JCM Guest

    Ivan Voras <ivoras@__geri.cc.fer.hr> wrote:
    > What is the 'most pythonic' way of searching the largest element in a
    > list/tuple?


    > My 'standard' idea is:


    > max = list[0] # or -infinity, or whatever...
    > for i in list:
    > if i > max:
    > max = i


    > While this is ok, I somehow 'feel' there could be a more concise
    > solution... :)


    reduce(lambda x, y: (x, y)[x < y], list)

    Sorry, couldn't resist.
     
    JCM, Mar 4, 2004
    #5
  6. Ivan Voras

    Ivan Voras Guest

    JCM wrote:

    > reduce(lambda x, y: (x, y)[x < y], list)
    >
    > Sorry, couldn't resist.


    Actually, I thought that the 'better' way would be using a lambda
    function but then again, I was waaay wrong :)))

    Thanks, all :)
     
    Ivan Voras, Mar 5, 2004
    #6
  7. Catching up...

    * Ivan Voras (2004-03-04 22:52 +0100)
    > What is the 'most pythonic' way of searching the largest element in a
    > list/tuple?
    >
    > My 'standard' idea is:
    >
    > max = list[0] # or -infinity, or whatever...
    > for i in list:
    > if i > max:
    > max = i
    >
    > While this is ok, I somehow 'feel' there could be a more concise
    > solution... :)


    max(seq)

    In a real world example you wouldn't be interested in "the" maximum
    but in the extrema/maxima according to function f (in your case the
    identity f(x)=x), which could be more than just one item.

    Thorsten
     
    Thorsten Kampe, Apr 28, 2004
    #7
    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. Vianney =?iso-8859-1?Q?Lan=E7on?=

    Calcule l element maximum d'un tableau

    Vianney =?iso-8859-1?Q?Lan=E7on?=, Aug 28, 2003, in forum: C++
    Replies:
    1
    Views:
    410
    Artie Gold
    Aug 29, 2003
  2. Hexamorph

    Re: Index of maximum element in list

    Hexamorph, Jan 25, 2008, in forum: Python
    Replies:
    15
    Views:
    587
    Arnaud Delobelle
    Jan 27, 2008
  3. HANM
    Replies:
    2
    Views:
    767
    Joseph Kesselman
    Jan 29, 2008
  4. Feyruz
    Replies:
    15
    Views:
    230
    Tad McClellan
    Nov 30, 2005
  5. phanhuyich
    Replies:
    4
    Views:
    333
Loading...

Share This Page