'int' object is not iterable when I'm not trying to iterate

末鹿安然 提交于 2019-12-11 05:06:41

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/5729068/int-object-is-not-iterable-when-im-not-trying-to-iterate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!