'int'当我不尝试迭代时,对象不可迭代
以下代码尝试创建一个地图,显示从该地图上的每个方块到指定位置所需的最少移动次数。整个函数在很大程度上与问题无关,但我认为我应该在上下文中提供我的问题。我还从集合中导入了双端队列。奇怪的部分出现在第 7 行。我得到 TypeError: 'int' object not iterable。但语句“distance_from_loc, f_loc = squares_to_check.popleft()”不应该尝试迭代任何内容。任何帮助将不胜感激。
def complex_distance(self, loc):
row, col = loc
squares_to_check = deque((0, loc))
self.complex_distance_map = zeros((self.height, self.width), int) + 999
self.complex_distance_map[row][col] = 0
while squares_to_check:
distance_from_loc, f_loc = squares_to_check.popleft()
distance_from_loc += 1
for d in AIM:
n_loc = self.destination(f_loc, d)
n_row, n_col = n_loc
if distance_from_loc < self.complex_distance_map[n_row][n_col] and not self.map[n_row][n_col] == -4:
squares_to_check.append((distance_from_loc, n_loc))
self.complex_distance_map[n_row][n_col] = distance_from_loc
The following piece of code attempts to create a map that shows the minimum number of moves it would take to get from each square on that map to the specified location. The function as a whole is largely irrelevant to the problem, but I thought I should provide my problem in context. I've also imported deque from collections. The strange part comes in at line 7. I get TypeError: 'int' object not iterable. But the statement "distance_from_loc, f_loc = squares_to_check.popleft()" shouldn't be attempting to iterating anything to the best of knowledge. Any help would be greatly appreciated.
def complex_distance(self, loc):
row, col = loc
squares_to_check = deque((0, loc))
self.complex_distance_map = zeros((self.height, self.width), int) + 999
self.complex_distance_map[row][col] = 0
while squares_to_check:
distance_from_loc, f_loc = squares_to_check.popleft()
distance_from_loc += 1
for d in AIM:
n_loc = self.destination(f_loc, d)
n_row, n_col = n_loc
if distance_from_loc < self.complex_distance_map[n_row][n_col] and not self.map[n_row][n_col] == -4:
squares_to_check.append((distance_from_loc, n_loc))
self.complex_distance_map[n_row][n_col] = distance_from_loc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该行does尝试迭代:
该行
使用两个元素
0
和loc
初始化双端队列,而不是使用单个元素( 0,loc)。使用
以获得所需的结果。
The line does try to iterate:
The line
initialises the deque with the two elements
0
andloc
, not with the single element(0, loc)
. Useto get the desired result.