python 类里面的函数总是报错显示没有定义
importmatplotlib.pyplotaspltclassTSP:def__init__(self,filename):self.MyList=[]f=open(...
import matplotlib.pyplot as plt
class TSP:
def __init__(self,filename):
self.MyList = []
f = open(filename)
for line in f:
l = line.split(';') # Could be ',' depending on your system
for i in range(0,len(l)):
l[i] = l[i].rstrip('\n')
self.MyList.append(l)
f.close()
self.matrix = buildMatrix()
self.tour = []
def buildMatrix(self):
m=zeros([cn,cn],int16)
for i in range(0,cn):
for j in range(0,cn):
m[i][j]=self.distance(i,j)
return m
def distance(self,i,j):
d=(self.city[i][1]-self.city[j][1])**2+(self.city[i][2]-self.city[j][2])**2
d=d**(1/2)
t = TSP('tour26.csv')
print(t.matrix)
Traceback (most recent call last):
File "D:\Python34\tem2.py", line 36, in <module>
t = TSP('tour26.csv')
File "D:\Python34\tem2.py", line 18, in __init__
self.matrix = buildMatrix()
NameError: name 'buildMatrix' is not defined 展开
class TSP:
def __init__(self,filename):
self.MyList = []
f = open(filename)
for line in f:
l = line.split(';') # Could be ',' depending on your system
for i in range(0,len(l)):
l[i] = l[i].rstrip('\n')
self.MyList.append(l)
f.close()
self.matrix = buildMatrix()
self.tour = []
def buildMatrix(self):
m=zeros([cn,cn],int16)
for i in range(0,cn):
for j in range(0,cn):
m[i][j]=self.distance(i,j)
return m
def distance(self,i,j):
d=(self.city[i][1]-self.city[j][1])**2+(self.city[i][2]-self.city[j][2])**2
d=d**(1/2)
t = TSP('tour26.csv')
print(t.matrix)
Traceback (most recent call last):
File "D:\Python34\tem2.py", line 36, in <module>
t = TSP('tour26.csv')
File "D:\Python34\tem2.py", line 18, in __init__
self.matrix = buildMatrix()
NameError: name 'buildMatrix' is not defined 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询