我是python的新手。我在此代码中遇到了一些问题,该代码是项目的一部分。列表v,h,lx与主要数据相似。错误在第15行:“ ValueError:0不在列表中”。我无法理解解决此错误的原因。代码如下。
v = [(0,5), (2,5), (3,2), (4,2), (5,5)]
h = [(5,0), (5,2), (2,3), (2,4), (5,5)]
lx = [(0, 0), (0, 2), (0, 3), (0, 4), (0, 5), (2, 0), (2, 2), (2, 3), (2, 4), (2, 5), (3, 0), (3, 2), (4, 0), (4, 2), (5, 0), (5, 2), (5, 5)]
#ly= [(0, 0), (2, 0), (3, 0), (4, 0), (5, 0), (0, 2), (2, 2), (3, 2), (4, 2), (5, 2), (0, 3), (2, 3), (0, 4), (2, 4), (0, 5), (2, 5), (5, 5)]
ly = sorted(lx, key = lambda q : q[1])
for i in range(len(lx)):
x0 = lx[i][0]
y0 = lx[i][1]
if((x0,y0) in v or (x0,y0) in h):
continue
else:
for j in range(1, len(v)):
x1 = ly[ly.index(x0, y0) + j][0]
if ((x1,y0) in v):
continue
else:
pass
for k in range(1, len(h)):
y1 = lx[lx.index(x0,y0) + k][1]
if ((x0,y1) in h):
continue
else:
pass
print(x0,y0,x1,y1)
错误:
Traceback (most recent call last):
File "C:\Users\ASUS\Desktop\python\test2.py", line 15, in <module>
x1 = ly[ly.index(x0, y0) + j][0]
ValueError: 0 is not in list
预期输出:
0 0 2 2
0 2 2 3
0 3 2 4
0 4 2 5
.......
答案 0 :(得分:1)
index((x0,y0))而不是index(x0,y0)