I'm working on a name list cnt
that has this form,
[[[1555 1379]]
[[1554 1380]]
...
[[1549 1380]]]
The first value of each row is the X coordinate and the second is the Y coordinate. My idea is to obtain the maximum and minimum value of each axis.
I'm trying like this, but I'm making a mistake (I think) in the index.
x0, x1 = np.min(cnt[:, 0]), np.max(cnt[:, 0])
y0, y1 = np.min(cnt[:, 1]), np.max(cnt[:, 1])
I've also tried this but it doesn't work for me.
x0, x1 = np.min(cnt[0]), np.max(cnt[0])
y0, y1 = np.min(cnt[1]), np.max(cnt[1])
Can somebody help me? Thanks.
By
print
what you display and how you index,cnt
it's not a list (list
) it's a three-dimensional NumPy array. In such a case use the argumentaxis
ofnumpy.min
ynumpy.max
:Regarding your attempt, the problem is that with you
cnt[:, 0]
select all the rows (:
) and of them the first column (0
), the only one that has the array,cnt[:, 1]
is therefore invalid. You should be in any case:Related Finding max value in the second column of a nested list?
Try this:
How does it work?
The module
min y max
has an optional argumentkey
that executes a function for each element of the list, and since you have a list within a list, what the module doeslambda
is declare on the same line (for convenience) a function that in this case in particular returns element [n] of your axis list inside your array and passes it tomin y max
respectively.min y max
returns the element of the list that matches the maximum and minimum so the "[0] and [1]" at the end of each line specify that you want the first and second value of each axis.Example: