所以我正在为一个更大的项目制作一本波士顿地铁站及其邻居的字典。每个车站都属于一条线,就像蓝色或红色一样,这些线构成了波士顿整个地铁系统。但我遇到了麻烦。到目前为止,这是我的代码:
for i in range(len(tuples)):
for x in range(len(tuples[i][1])):
print tuples
neighbors=[]
station=tuples[i][1][x]
if tuples[i][1][-1]==station and station not in duplicates:
line=0
for item in tuples:
if station in item[1]:
line=item[0]
break
neighbors.append((tuples[i][1][-2],line))
elif tuples[i][1][0]==station and station not in duplicates:
line=0
for item in tuples:
if station in item[1]:
line=item[0]
break
neighbors.append((tuples[i][1][1],line))
elif station in duplicates:
line=[]
for item in tuples:
if station in item[1]:
line.append((item[0],tuples.index(item)))
neighbors=[(tuples[i][1][x+1],tuples[i][1][x-1],line[0][0]),(tuples[line[1][1]][1][x+1],tuples[line[1][1]][1][x-1],line[1][0])]
else:
line=0
for item in tuples:
if station in item[1]:
line=item[0]
break
neighbors.append((tuples[i][1][x+1],tuples[i][1][x-1],line[0][0]))
for neighbor in neighbors:
subdict[station]={}
for i in range(len(neighbor)-1):
subdict[station].update({neighbor[i]:neighbor[-1]})
所以我运行它并得到一个语法错误,指示索引超出范围错误。这没有任何意义,但后来我以元组的形式打印出各个站点和列表,发现它们一直在变化。所以在一次迭代中,一条地铁线路的站点列表与预期的相同,但之后就完全不同了。只有一条地铁线路列表中的第一站保持不变;其余的来自波士顿地铁的不同线路。
那么有人可以告诉我我的代码中发生了什么吗?