max value in list

Discussion in 'Python' started by C GIllespie, May 12, 2004.

  1. C GIllespie

    C GIllespie Guest

    Dear all,

    I have a few lists. What's the best way of finding the maximum number of all
    the lists.

    For example,

    [2,3,4],[1,2,3],[4,7]

    The max is 7.

    Thanks

    Colin
    C GIllespie, May 12, 2004
    #1
    1. Advertising

  2. C GIllespie

    Terry Reedy Guest

    "C GIllespie" <> wrote in message
    news:c7te26$8q1$...
    > Dear all,
    >
    > I have a few lists. What's the best way of finding the maximum number of

    all
    > the lists.
    >
    > For example,
    >
    > [2,3,4],[1,2,3],[4,7]
    >
    > The max is 7.


    Sidestepping 'best' which may depend on context, a general 'good' way is

    >>> lists = [2,3,4],[1,2,3],[4,7]
    >>> max([max(l) for l in lists])

    7

    Terry J. Reedy
    Terry Reedy, May 12, 2004
    #2
    1. Advertising

  3. [[ This message was both posted and mailed: see
    the "To," "Cc," and "Newsgroups" headers for details. ]]


    What about:

    m = max([max(i) for i in (list1, list2, list3, etc...)])

    Example:

    >>> max([max(i) for i in ([2,3,4], [1,2,3], [4,7])])

    7

    /Jean Brouwers
    ProphICy Semiconductor, Inc.


    In article <c7te26$8q1$>, C GIllespie
    <> wrote:

    > Dear all,
    >
    > I have a few lists. What's the best way of finding the maximum number of all
    > the lists.
    >
    > For example,
    >
    > [2,3,4],[1,2,3],[4,7]
    >
    > The max is 7.
    >
    > Thanks
    >
    > Colin
    >
    >
    Jean Brouwers, May 12, 2004
    #3
  4. C GIllespie

    Peter Abel Guest

    "C GIllespie" <> wrote in message news:<c7te26$8q1$>...
    > Dear all,
    >
    > I have a few lists. What's the best way of finding the maximum number of all
    > the lists.
    >
    > For example,
    >
    > [2,3,4],[1,2,3],[4,7]
    >
    > The max is 7.
    >
    > Thanks
    >
    > Colin


    >>> l=[[2, 3, 4], [1, 2, 9], [4, 7]]
    >>> max(map(max,l))

    9
    >>> l=[[2, 3, 4], [1, 2, 9],[12], [4, 7]]
    >>> max(map(max,l))

    12
    >>>


    Regards
    Peter
    Peter Abel, May 12, 2004
    #4
  5. C GIllespie

    Peter Otten Guest

    C GIllespie wrote:

    > I have a few lists. What's the best way of finding the maximum number of
    > all the lists.
    >
    > For example,
    >
    > [2,3,4],[1,2,3],[4,7]
    >
    > The max is 7.


    For arbitrarily nested sequences:

    >>> def rmax(seq):

    .... try:
    .... return max(map(rmax, seq))
    .... except TypeError:
    .... return seq
    ....
    >>> rmax(1)

    1
    >>> rmax([1,2])

    2
    >>> rmax([[1,2], [3,4]])

    4
    >>> rmax([[1,2], [3,44], 5, [6]])

    44
    >>> rmax([[1,2], [[3,44], 5, [6]]])

    44
    >>>


    Peter
    Peter Otten, May 12, 2004
    #5
  6. C GIllespie

    Duncan Booth Guest

    "Terry Reedy" <> wrote in
    news::

    > Sidestepping 'best' which may depend on context, a general 'good' way is
    >
    >>>> lists = [2,3,4],[1,2,3],[4,7]
    >>>> max([max(l) for l in lists])

    > 7
    >


    Without any claims to better or worse, here is another option:

    >>> l = [2,3,4],[1,2,3],[4,7]
    >>> l

    ([2, 3, 4], [1, 2, 3], [4, 7])
    >>> max(itertools.chain(*l))

    7
    >>>
    Duncan Booth, May 12, 2004
    #6
  7. C GIllespie

    C GIllespie Guest

    Dear All,

    Thanks for all the responses.

    Very helpful (again)

    Colin
    C GIllespie, May 13, 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. jj_frap
    Replies:
    6
    Views:
    383
    Steven Bethard
    Jun 5, 2006
  2. Summercool
    Replies:
    9
    Views:
    879
    dorayme
    Oct 23, 2007
  3. W. eWatson
    Replies:
    8
    Views:
    218
  4. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    576
    Greg Ferris
    Jan 16, 2004
  5. kriton
    Replies:
    0
    Views:
    334
    kriton
    Apr 22, 2005
Loading...

Share This Page