BOMBOLOM.COM

(python) Find minimum and maximum value of a set of lists

Posted by José Lopes

This post shows a range of solutions to find the maximum value of a set of lists, and the minimum value by analogy.

Supose, as an example, that you have the following lists:

A = [1, 2, 9]
B = [100, 50, 75]
C = [6, 2, 80]
D = [42, 33, 5]

We want to get 100 as the maximum and 1 as the minimum values.

Python's builtin functions that allow us to get these values are:

I'll show next some ways to ge the maximum value, being that for the minimum you just have to change max by min.
The solution you choose depends on the problem to solve and your personal programing style.

Solution 1

Lists = A,B,C,D
max([max(i) for i in Lists])

Solution 2

max([max(i) for i in (A, B, C, D)])

Solution 3

Lists = A,B,C,D
max(map(max,Lists))

Solution 4

Create a function:

def get_max(seq):
    try:
        return max(map(get_max, seq))
    except TypeError:
        return seq

To use it:

Lists = A,B,C,D
get_max(Lists)
# or:
get_max([A,B,C,D])

Solution 5

Lists = A,B,C,D
import itertools
max(itertools.chain(*Lists))
2007.09.04 | There's more... | Comments 0 | Tags ,

Deixe a sua mensagem:

Nome:


E-mail:


URL:


Comment:

Secret number

To send you comment you must insert the "secret number" on the box


Made with PyBlosxom | Add to Google