Not Yet Answered # Comparing a matrix(list[][]) ?

jrcagle 77 Ene Uran 638 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

0

Hi, this is my first post, and im trying to compare a matrix to find the minus element greater than zero, my list 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 is associated to (X,Y) position.

Thanks a lot !

jDSL

I'm confused by your desire not to use min -- are you saying that (unlike the example) -- each element is a member of R2? Perhaps a complex number?

Anyways, for the matrix above, you could do something like this:

min_value = min([min([elem for elem in row if elem > 0]) for row in matrix])

This creates a list of the minimums of the non-zero elements in each row, then finds the minimum of that list.

Example:

```
>>> def min_value(matrix):
return min([min([elem for elem in row if elem > 0]) for row in matrix])
>>> matrix=[[9,8,12,15],\
[0,11,15,18],\
[0,0,10,13],\
[0,0,0,5]]
>>> matrix
[[9, 8, 12, 15], [0, 11, 15, 18], [0, 0, 10, 13], [0, 0, 0, 5]]
>>> min_value(matrix)
5
```

If the elements don't properly behave under min, you could roll your own min function. However, it might be the case that there is no well-defined min for your type of object -- points in 2-space, for example, don't have a well-defined comparison function.

Hope it helps,

Jeff

0

Similar to Jeff's code:

```
# write this as a list of lists ...
matrix = [[9,8,12,15],
[0,11,15,18],
[0,0,10,13],
[0,0,0,5]]
# find the minimum value above val in each list
val = 0
for n in matrix:
print n # test
mv = min([v for v in n if v > val])
print mv
"""
my output -->
[9, 8, 12, 15]
8
[0, 11, 15, 18]
11
[0, 0, 10, 13]
10
[0, 0, 0, 5]
5
"""
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I don’t want at this stage work on a big separate project as I've already got plenty ...