python 中随机库的索引错误。适用于小输入但不适用于大输入
Index Error for random library in python. Works for small inputs but doesn't work for large inputs
我必须创建一个自我回避格子路径,它在 4 个方向(北、东、西、南)移动。
我为此编写了一个函数:
n=int(input())
edges = [[1, 0], [0, 1], [-1, 0], [0, -1]]
make_step = lambda point, edge: [x+y for x,y in zip(point, edge)]
path=[[0,0]]
x_coo=[0]
y_coo=[0]
for i in range(n):
next_points = [make_step(path[-1], edge) for edge in edges]
allowed_points = [point for point in next_points if point not in path]
p=rand.choice(allowed_points)
path.append(p)
x_coo.append(p[0])
y_coo.append(p[1])
plt.plot(x_coo,y_coo)
在输入为 60 之前它运行良好,但当输入大于 0 时会出错。
IndexError Traceback (most recent call last)
<ipython-input-15-a4a59bcce6b0> in <module>
10 allowed_points = [point for point in next_points if point not in path]
11
---> 12 p=rand.choice(allowed_points)
13
14 path.append(p)
~\anaconda3\lib\random.py in choice(self, seq)
259 i = self._randbelow(len(seq))
260 except ValueError:
--> 261 raise IndexError('Cannot choose from an empty sequence') from None
262 return seq[i]
263
IndexError: Cannot choose from an empty sequence
有人请帮我解决这个错误。
你的代码没有问题,只是因为指针遇到了死胡同:无论往哪个方向移动,都会走上之前的路径。在这种情况下,allowed_points
将为空。
我通过添加 if
语句修改了您的代码,因此它会自动停止并且 return 一旦进入死胡同的步数:
for i in range(n):
next_points = [make_step(path[-1], edge) for edge in edges]
allowed_points = [point for point in next_points if point not in path]
if allowed_points!=[]:
p=rand.choice(allowed_points)
path.append(p)
x_coo.append(p[0])
y_coo.append(p[1])
else:
print ('dead end, stops when n=',i)
break
在我设置input=100的一条trail中,它return是以下路径,当n=70时停止:
如您所见,指针停留在 (4,12);任何一步都会与之前的路径之一重叠 [(3,12), (5,12), (4,11), (4,13)].
我必须创建一个自我回避格子路径,它在 4 个方向(北、东、西、南)移动。 我为此编写了一个函数:
n=int(input())
edges = [[1, 0], [0, 1], [-1, 0], [0, -1]]
make_step = lambda point, edge: [x+y for x,y in zip(point, edge)]
path=[[0,0]]
x_coo=[0]
y_coo=[0]
for i in range(n):
next_points = [make_step(path[-1], edge) for edge in edges]
allowed_points = [point for point in next_points if point not in path]
p=rand.choice(allowed_points)
path.append(p)
x_coo.append(p[0])
y_coo.append(p[1])
plt.plot(x_coo,y_coo)
在输入为 60 之前它运行良好,但当输入大于 0 时会出错。
IndexError Traceback (most recent call last)
<ipython-input-15-a4a59bcce6b0> in <module>
10 allowed_points = [point for point in next_points if point not in path]
11
---> 12 p=rand.choice(allowed_points)
13
14 path.append(p)
~\anaconda3\lib\random.py in choice(self, seq)
259 i = self._randbelow(len(seq))
260 except ValueError:
--> 261 raise IndexError('Cannot choose from an empty sequence') from None
262 return seq[i]
263
IndexError: Cannot choose from an empty sequence
有人请帮我解决这个错误。
你的代码没有问题,只是因为指针遇到了死胡同:无论往哪个方向移动,都会走上之前的路径。在这种情况下,allowed_points
将为空。
我通过添加 if
语句修改了您的代码,因此它会自动停止并且 return 一旦进入死胡同的步数:
for i in range(n):
next_points = [make_step(path[-1], edge) for edge in edges]
allowed_points = [point for point in next_points if point not in path]
if allowed_points!=[]:
p=rand.choice(allowed_points)
path.append(p)
x_coo.append(p[0])
y_coo.append(p[1])
else:
print ('dead end, stops when n=',i)
break
在我设置input=100的一条trail中,它return是以下路径,当n=70时停止:
如您所见,指针停留在 (4,12);任何一步都会与之前的路径之一重叠 [(3,12), (5,12), (4,11), (4,13)].