'Code for finding if a "point" (x,y) in between a user inputed, xmax,xmin,ymax,ymin and printing "point" name using flow control commands
Given a nested list of points and locations (x,y): pointlist = [[[-174.6, 52], 'A'], [[-152, 58], 'B'], [[-166.1, 53], 'C']],[[90, -179.7], 'D']]
I need help creating a code that will tell if a point is between a user inputted xmax, xmin, ymax, ymin. If a point from the pointlist is in the range of both the xmax-xmin and ymax-ymin values then the code needs to print the name or names of the point or points included.
The format of the data list is pointlist= [[x value, y value], 'point name']
I have defined the variables that the user will input:
xmax = float(input("Enter a maximum x value:"))
xmin = float(input("Enter a minimum x value:"))
ymax = float(input("Enter a maximum y value:"))
ymin = float(input("Enter a minimum y value:"))
I know I need to use If and For statements, and => and <= but I am not sure how to relate that with the negative values involved in the data set. Our teacher wants to be able to use this code on a bigger data set. The one she gave us above is just a small sample size for practice.
Solution 1:[1]
Here is my attempt. It outputs as a list:
pointlist = [[[-174.6, 52], 'A'], [[-152, 58], 'B'], [[-166.1, 53], 'C'],[[90, -179.7], 'D']]
def check(xmax, xmin, ymax, ymin):
in_range = []
for i in pointlist:
if i[0][0] >= xmin and i[0][0] <= xmax and i[0][1] >= ymin and i[0][1] <= ymax:
in_range.append(i[1])
return in_range
Some examples:
>>>print(check(100, -180, 60, -180))
['A', 'B', 'C', 'D']
>>>print(check(100, -170, 60, -180))
['B', 'C', 'D']
>>>print(check(80, -180, 60, -180))
['A', 'B', 'C']
>>>print(check(100, -180, 55, -180))
['A', 'C', 'D']
>>>print(check(100, -180, 60, -170))
['A', 'B', 'C']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | decker520 |
