-1

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
JDT198
  • 3
  • 1

1 Answers1

0

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']
decker520
  • 16
  • 1
  • You could also pass the point list in as an argument for more versatility (but you shouldn't ask homework questions haha). – decker520 Feb 12 '19 at 04:43