0

I'm currently trying to build a class which contain two functions- first function will construct a list and the second function will search through the list and return some of the values sorted. however I am having difficulty trying to reference the list created in the first function ('points') in the second function. can anyone inform me where I'm going wrong here?

class naiveNN(nearestneigh):

 def build_index(self, points: [Point]):
       points = []
       with open('sampleData.txt', 'r') as file:
           for line in file:
               line = line.strip('\n')
               line = line.split(' ')
               point_id = line[0]
               cat = line[1]
               lat = line[2]
               lon = line[3]
               point = Point(point_id, cat, lat, lon)
               points.append(point)
       return points


   def search(self, search_term: Point, k: int) -> [Point]:
       distance_neigh = []
       for i, point in enumerate(*points*):
           distance = Point.dist_to(point, search_term)
           distance_neigh.append(distance)
       sorted_nigh = sorted(distance_neigh)[:k]
       return sorted_nigh
vitaminSí
  • 39
  • 5
  • Just to note, `build_index` can be reduced to `with open(...) as file: return [Point(line.strip('\n').split(' ')[:3]) for line in file]`, though the longer form is more amenable to error checking. – chepner Sep 10 '21 at 13:35
  • Thanks! yep I'm just keeping it spread out so I can change some parts later on but thank you for the suggestions. – vitaminSí Sep 10 '21 at 13:45

1 Answers1

0

Call it self.points. This way, it will be an attribute of the naiveNN object, and all its methods will be able to manipulate it.

Jakob Filser
  • 131
  • 9