Home > other >  How do I reference a list from another function? [duplicate]
How do I reference a list from another function? [duplicate]

Time:09-17

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

CodePudding user response:

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.

  • Related