【发布时间】:2022-01-23 20:02:04
【问题描述】:
在这段代码中,我无法访问 while 循环内的队列元素。 它告诉我
'int' 对象不可下标
但是如果我在 while 循环之外访问它,那么它工作正常。 请给出一个想法,我该如何解决这个问题?
代码:
queue.put([0,1])
sort_path[1]=0;
a = queue.get()
print(a[0])
while not queue.empty():
N = queue.get()
weight = N[0]
node = N[1]
for i in graph[node]:
if(weight+i[1]<sort_path[i[0]]):
sort_path[i[0]]=weight+i[1]
queue.put(sort_path[i[0]],sort_path[i[0]])
print(sort_path)
【问题讨论】:
-
此错误消息告诉您对象
N是int,而您正在尝试使用N[0],它通常用于访问序列或映射中的项目。此外,您的回溯与显示的代码不匹配,因为它不包含int()调用。print()或以其他方式调查N的值。 -
错误告诉你N是一个int,不是元组或列表。请发布代码而不是发布图像。我无法通过图片重现您的问题。
-
当变量出现问题时,使用简单的调试选项
print(N)确保变量符合您的想法 -
看来你从队列中得到的 N 值只是一个整数,我猜这不是你期望的数据
-
为什么要放两次
sort_path[i[0]]的元组这里不是有错字吗?
标签: python arrays python-3.x list