'int'当我不尝试迭代时,对象不可迭代

发布于 2024-11-02 18:14:17 字数 1007 浏览 5 评论 0原文

以下代码尝试创建一个地图,显示从该地图上的每个方块到指定位置所需的最少移动次数。整个函数在很大程度上与问题无关,但我认为我应该在上下文中提供我的问题。我还从集合中导入了双端队列。奇怪的部分出现在第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2024-11-09 18:14:17

该行does尝试迭代:

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

该行

squares_to_check = deque((0, loc))

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文