我正在为一个排班问题编码,我必须在一些场所安排培训师和老师。到目前为止,我已经编写了代码以找出距培训师最近的三个距离。现在,我想为所有在场的培训人员动态创建该类的多个对象。但是我只能创建一个对象。在这段代码中,我从名为mtData.csv
和venueData.csv
的两个csv文件中导入了数据,请帮助我消除错误。
我试图逐个循环地制作对象。在这两种情况下,我都遇到错误。
import math
import csv
from math import radians, sin, cos, acos
class venue:
venueGeoLat = []
venueGeoLon = []
venueName = []
def __init__(self, name, latitude, longitude):
self.name = name
self.latitude = latitude
self.longitude = longitude
with open('venueData.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
venueGeoLat.append(row[1])
venueGeoLon.append(row[2])
venueName.append(row[0])
class masterTrainer:
name = None
location = None
subject = None
latitude = None
longitude = None
distanceFromVenues = []
nearestVenues = []
def __init__(self, name, location, subject, latitude, longitude):
masterTrainer.name = name
masterTrainer.location = location
masterTrainer.subject = subject
masterTrainer.latitude = latitude
masterTrainer.longitude = longitude
x = 1
y = 1
while (x < 2):
while (y < len(venue.venueGeoLat)):
masterTrainer.distanceFromVenues.append(masterTrainer.distanceCalculator(float(masterTrainer.latitude),float(masterTrainer.longitude),float(venue.venueGeoLat[y]),float(venue.venueGeoLon[y])))
y = y + 1
x = x + 1
MAX = 100000
firstmin = MAX
secmin = MAX
thirdmin = MAX
k = 0
l = 0
m = 0
for i in range(0, len(masterTrainer.distanceFromVenues)):
if masterTrainer.distanceFromVenues[i] < firstmin:
thirdmin = secmin
secmin = firstmin
firstmin = masterTrainer.distanceFromVenues[i]
k = i
elif masterTrainer.distanceFromVenues[i] < secmin:
thirdmin = secmin
secmin = masterTrainer.distanceFromVenues[i]
l = i
elif masterTrainer.distanceFromVenues[i] < thirdmin:
thirdmin = masterTrainer.distanceFromVenues[i]
m = i
masterTrainer.nearestVenues.append(venue.venueName[k+1])
masterTrainer.nearestVenues.append(venue.venueName[l+1])
masterTrainer.nearestVenues.append(venue.venueName[m+1])
def distanceCalculator(latitude1,longitude1,latitude2,longitude2):
slat = radians(latitude1)
slon = radians(longitude1)
elat = radians(latitude2)
elon = radians(longitude2)
dist = 6371.01 * acos(sin(slat)*sin(elat) + cos(slat)*cos(elat)*cos(slon - elon))
return dist
name = []
location = []
subject = []
latitude =[]
longitude = []
with open('mtData.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
name.append(row[0])
location.append(row[1])
subject.append(row[4])
latitude.append(row[2])
longitude.append(row[3])
mt1 = masterTrainer(name[0],location[0],subject[0],latitude[0],longitude[0])
print(mt1.nearestVenues)
mt2 = masterTrainer(name[1],location[1],subject[1],latitude[1],longitude[1])
print(mt2.nearestVenues)
print(mt1.nearestVenues)
print(mt2.distanceFromVenues)
我收到索引错误IndexError: list index out of range
完全追溯
<ipython-input-115-2ee0ce48bf78> in <module>
103 mt1 = masterTrainer(name[0],location[0],subject[0],latitude[0],longitude[0])
104 print(mt1.nearestVenues)
--> 105 mt2 = masterTrainer(name[1],location[1],subject[1],latitude[1],longitude[1])
106 print(mt2.nearestVenues)
107 print(mt1.nearestVenues)
<ipython-input-115-2ee0ce48bf78> in __init__(self, name, location, subject, latitude, longitude)
74 masterTrainer.nearestVenues.append(venue.venueName[k+1])
75 masterTrainer.nearestVenues.append(venue.venueName[l+1])
---> 76 masterTrainer.nearestVenues.append(venue.venueName[m+1])
77
78
IndexError: list index out of range```