Comparing a matrix (list[][]) ?

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Sat Jan 13 18:57:56 EST 2007


jairodsl wrote:
> How can I find the minus element greater than zero in a matrix, my
> matrix is
> 
> matrix=
> [9,8,12,15],
> [0,11,15,18],
> [0,0,10,13],
> [0,0,0,5]
> 
> I dont want to use "min" function because each element in the matrix is
> associated to (X,Y) position.

What output are you expecting from your example matrix?  If you are expecting
it to be 5 (the smallest positive element), using "min" is the way to do it:

    >>> matrix = [[9,  8, 12, 15],
    ...           [0, 11, 15, 18],
    ...           [0,  0, 10, 13],
    ...           [0,  0,  0,  5]]
    >>> min(min(x for x in row if x > 0) for row in matrix)
    5

-- 
Roberto Bonvallet



More information about the Python-list mailing list