当我不尝试迭代时,“int"对象不可迭代 [英] 'int' object is not iterable when I'm not trying to iterate

查看:38
本文介绍了当我不尝试迭代时,“int"对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码尝试创建一个地图,显示从该地图上的每个方格到达指定位置所需的最少移动次数.整个函数在很大程度上与问题无关,但我认为我应该在上下文中提供我的问题.我还从集合中导入了 deque.奇怪的部分出现在第 7 行.我得到 TypeError: 'int' object not iterable.但是语句distance_from_loc, f_loc = squares_to_check.popleft()"不应该试图迭代任何知识.任何帮助将不胜感激.

 def complex_distance(self, loc):行,col = locsquares_to_check = deque((0, loc))self.complex_distance_map = zeros((self.height, self.width), int) + 999self.complex_distance_map[row][col] = 0而 squares_to_check:distance_from_loc, f_loc = squares_to_check.popleft()distance_from_loc += 1对于 AIM 中的 d:n_loc = self.destination(f_loc, d)n_row, n_col = n_loc如果 distance_from_loc 

解决方案

does 尝试迭代的行:

<预><代码>>>>a, b = 0回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:int"对象不可迭代

线

squares_to_check = deque((0, loc))

使用两个元素 0loc 初始化双端队列,而不是使用单个元素 (0, loc).使用

squares_to_check = deque([(0, 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

解决方案

The line does try to iterate:

>>> a, b = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

The line

squares_to_check = deque((0, loc))

initialises the deque with the two elements 0 and loc, not with the single element (0, loc). Use

squares_to_check = deque([(0, loc)])

to get the desired result.

这篇关于当我不尝试迭代时,“int"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆