我写了这个类来编织两行。
class CoordinatRow(object):
def __init__(self):
self.coordinaterow = []
def add_coordinaterow(self, coordinate):
self.coordinaterow.append(coordinate)
def weave(self,other):
lijst = []
for i in range(len(self.coordinaterow)):
lijst.append(self.coordinaterow[i])
lijst.append(other.coordinaterow[i])
self.coordinaterow = lijst
return self.coordinaterow
我将坐标添加到对象中:
def process_row(line):
temp_coordinate_row = CoordinatRow()
rij = line.split()
for coordinate in rij:
coor = process_coordinate(coordinate)
temp_coordinate_row.add_coordinaterow(coor)
return temp_coordinate_row
我想通过这样做来添加2行:
def split_and_add(invoer):
rij = invoer.split('=')
rows = []
for line in rij:
rows.append(process_row(line))
return rows
bestand = file_input()
rows = split_and_add(bestand)
for row in range(0,len(rows)-1):
rows[row].weave(rows[row+1])
但是我得到了以下错误:
rows[row].weave(rows[row+1])
lijst.append(other.coordinaterow[i])
IndexError: list index out of range
我该如何解决这个问题?
答案 0 :(得分:1)
你可以这样做来解决它:
for i in range(len(self.coordinaterow)):
lijst.append(self.coordinaterow[i])
try:
lijst.append(other.coordinaterow[i])
except IndexError:
pass