startswithH
startswithH

Reputation: 339

How to get the max x and min y coordinates from a 2D list in python?

I have a list that looks like:

[[8.91636727 0.50420552]
 [1.50918535 8.43128826]
 [4.18447757 0.21850886]
 [8.82701669 8.39773898]]

Essentially they are x,y coordinates and I wanted to know how to get the highest x with the lowest y. (i.e. 8.91.. and 0.50..). I started with the X's and thought of doing:

for x,y in means:
    if x >= start:
        start = x; h = x; l = y 
    else:
        start = start

But was wondering how to implement this for the min of y's. Also my other issue is that there may be a case such as:

[[8.91636727 0.50420552]
 [1.50918535 8.43128826]
 [4.18447757 0.21850886]
 [**8.92701669** 8.39773898]]

Where the I dont neccessarily always want the highest x by itself, I want the highest x coupled with the lowest y.

Upvotes: 2

Views: 7855

Answers (4)

mustafa candan
mustafa candan

Reputation: 846

For people who wants max and min coordinates

coors= [[8.91636727, 0.50420552], [1.50918535, 8.43128826], [4.18447757, 0.21850886], [8.82701669, 8.39773898]]

max_xy = max(coors, key=lambda x: (x[0], x[1]))
min_xy = min(coors, key=lambda x: (x[0], x[1]))

Upvotes: 0

startswithH
startswithH

Reputation: 339

My final approach was:

li = []
difference = 0
for x,y in means:
    dif = x - y
    if dif >= difference:
        difference = dif; h = x; l = y 
    else:
        difference = difference

Upvotes: 0

Rakesh
Rakesh

Reputation: 82765

This is one approach using max.

Ex:

data = [[8.91636727, 0.50420552], [1.50918535, 8.43128826], [4.18447757, 0.21850886], [8.82701669, 8.39773898]]
print(max(data, key=lambda x: (x[0], -x[1])))

Output:

[8.91636727, 0.50420552]

Upvotes: 3

Piotr Rarus
Piotr Rarus

Reputation: 952

You could calculate the difference between x and y vectors. Then choose the one with with highest difference. If this isn't what you're looking for, I must worry you. What you're talking about is multi-objective optimization. You must clearly define, what you mean by an element with highest x and lowest y. If you cannot put it into single function, it's fundamentally impossible to take that one element. You'd have to calculate your pareto front and take one element by hand from there.

Upvotes: 0

Related Questions