I have a list containing of lists containing of x- and y-coordinates:
[[x1, y1], [x2, y2], … [xn, yn]]
.
I want the smallest and the largest x- and y-coordinates.
Since the x-coordinates are always at index 0, I want to compare all values at index 0 and find the min and the max. Same for index 1 (y-coordinates).
My solution right now:
listX = []
listY = []
for i in list:
listX.append(i[0])
listY.append(i[1])
xmin = min(listX)
xmax = max(listX)
ymin = min(listY)
ymax = max(listY)
Is there a better way? For example "Comparing all values at index 0 and find the min" in one line? In total this would be four lines instead of eight.
I need this because I have a white image with not known shapes in it. For each shape I want to know the smallest rectangle there is to cover each shape completely. (4 shapes -> 4 rectangles not 1 rectangle for all). Different shapes can have the same color, but if that‘s the case they are not touching. Shapes of different colors could touch each other. The rectangles are allowed to overlap. This list in my question contains of all the pixels that are part of a certain shape.
This is slightly verbose, but you could use numpy to transpose the list so that all x values and y values are grouped in rows and then just get the min and max from each:
lst = [[1,2],[9,9],[2,2],[3,9]]
import numpy as np
def min_max(arr):
lst = np.array(arr).transpose()
min_x, max_x = np.min(lst[0]), np.max(lst[0])
min_y, max_y= np.min(lst[1]), np.max(lst[1])
return min_x, max_x, min_y, max_y
print(min_max(lst))
Outputs:
(1, 9, 2, 9)
lst = [[1,2],[9,9],[2,2],[3,9]]
def min_max2(arr):
min_x = arr[0][0]
min_y = arr[0][1]
max_x = arr[0][0]
max_y = arr[0][1]
for x, y in arr:
if x < min_x:
min_x = x
if y < min_y:
min_y = y
if x > max_x:
max_x = x
if y > max_y:
max_y = y
return min_x, max_x, min_y, max_y
print(min_max2(lst))
Outputs:
(1, 9, 2, 9)