试图让我的python代码计算csv中点之间的距离

2024-03-28 21:59:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一些读取csv的代码,并从该csv计算沿已选择的2个点之间的所有可能路径进行穷举搜索的最小成本,以及打印出选择的顺序点列表,以分割给出最小成本的路线

我现时的守则如下:

import csv import sys

size = 100 class PointGrabber:
    locationList = []
    def __init__(self, size):

        self.locationList = [] #Square brackets are an indexing operator
        self.matrix = [[0 for x in range(size)] for y in range (size)]
        self.points = []
        self.readData()
        self.storeDist()

    def readData(self):
        with open('/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip/Locations.csv') as f:
            csv_reader = csv.reader(f)
            index = 0
            next(csv_reader)
            for line in csv_reader:

               pointName = line[0]
               x = line[1]
               y = line[2]
               number = line[3]
               edge = line[4]
               
               water=False
               if line[5] == "W":
                   water=True
                
               self.locationList.append( [pointName,  x, y, number, edge, water] ) # etc
               
               if not pointName in self.points:
                   self.points.append(pointName)
               index += 1 
        f.close()

    def storeDist(self):
        for index in range(0, len(self.locationList)-1):

            if self.locationList[index][4] !="":
                start = self.locationList[index][0]
                end = self.locationList[index][4]

                for indexA in range(0, len(self.points)-1):
                    if self.points[indexA] == start:
                        indexPointA = indexA

                        for indexB in range(0, len(self.points)-1):
                            if self.points[indexB] == end:
                                indexPointB =indexB
                                distance = self.computeDist(start, end)
                                break
                        break

    def computePathDist(self, path):
        dist = 0
        return distance

    def findPath(self, a, b):
        path = []
        return path

    def computeCost(self, a, b):
        path = []
        costing = 0
        return costing, path 

    def computeDist(self, a, b):
        dist = 0
        return dist

    def improveDist(self, a, b):
        point=0
        scaledImprovement=0
        return point, scaledImprovement


    def findScope(self, a, b):
        pointList = [a, b]

        for index in range(0, size-1):
            if self.points[index ]== a:
                indexa=index
            if self.points[index] == b:
                indexb = index
            return pointList

if __name__ == '__main__':
    pg = PointGrabber(size)
    print(pg.locationList)

    pg.computeCost("15", "18")

    pg.improveDist("15", "18")