Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > max value in list

Reply
Thread Tools

max value in list

 
 
C GIllespie
Guest
Posts: n/a
 
      05-12-2004
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


 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      05-12-2004

"C GIllespie" <(E-Mail Removed)> wrote in message
news:c7te26$8q1$(E-Mail Removed)...
> 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




 
Reply With Quote
 
 
 
 
Jean Brouwers
Guest
Posts: n/a
 
      05-12-2004
[[ 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$(E-Mail Removed)>, C GIllespie
<(E-Mail Removed)> 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
>
>

 
Reply With Quote
 
Peter Abel
Guest
Posts: n/a
 
      05-12-2004
"C GIllespie" <(E-Mail Removed)> wrote in message news:<c7te26$8q1$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      05-12-2004
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

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      05-12-2004
"Terry Reedy" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
>>>

 
Reply With Quote
 
C GIllespie
Guest
Posts: n/a
 
      05-13-2004
Dear All,

Thanks for all the responses.

Very helpful (again)

Colin



 
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
want max width and max height as long as not exceed 100px Summercool HTML 9 10-23-2007 02:27 AM
ADSL Max/Max Gordy NZ Computing 6 11-24-2006 11:41 PM
Textarea max rows and max characters per row Greg Ferris Javascript 2 01-16-2004 07:21 PM
Max Apperture and Max. Shutter Speed Confusion-HELP bhaskar Digital Photography 12 07-22-2003 05:17 PM



Advertisments