'Finding minimum and maximum in a list of tuples i.e (min,max),(max,min)?
I have a list of tuples, (x1,y1),(x2,y2) etc and what I'd like to find is the tuple where x is a maximum and y is a minimum and various other combinations. I have been able to find where x is a maximum and y is a maximum, and when x is a minimum and y is a minimum, but I'm not sure how to first filter by maximum, then by minimum and vice versa.
Here's what I've tried so far:
coords = [(200,200),(40,150),(50,180),(20,200),(200,20),(20,20)]
c1 = max(coords, key=operator.itemgetter(0))
c2 = min(coords,key = operator.itemgetter(0))
c3 = max(coords,key = operator.itemgetter(1))
c4 = min(coords,key = operator.itemgetter(1))
#returns (200,200),(20,200),(200,200),(200,20))
#wanted (200,200),(20,200), (200, 20), (20,20)
#(max,max), (min,max), (max,min), (min,min) or some order of this
In reality 'coords' has more than 6 tuples. I end up with the same coordinate being returned for c1 and c3 for example when I wanted coords[0] and coords[3], so (max,max) and then (min,max). Am I over-complicating things, or is there a much simpler way to solve this? The order of the output doesn't particularly matter as long as it is the same order every time the coords change. I have simplified this issue to post here so there may be some errors sorry. Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
