1
以下代碼片段嘗試創建一個地圖,該地圖顯示從該地圖上的每個方形到指定位置所需的最小移動次數。作爲一個整體的功能在很大程度上與問題無關,但我認爲我應該在上下文中提供我的問題。我也從集合中導入了deque。奇怪的部分來自第7行。我得到TypeError:'int'對象不可迭代。但是,「distance_from_loc,f_loc = squares_to_check.popleft()」這個語句不應該試圖對任何事物進行迭代以達到最好的知識。任何幫助將不勝感激。當我不嘗試迭代時,'int'對象不可迭代
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
我的意圖是追加一個元組作爲deque中的第一個元素,就像我之後彈出這些值並分別將它們分配給distance_from_loc和f_loc。我所做的不應該等於a,b = 0,而應該是a,b =(0,0),這確實有效。 – 2011-04-20 13:17:12
@user:我想我明白了你的意圖。這就是爲什麼我指出如何正確初始化包含單個元組的deque。第一部分只是爲了說明你的錯誤信息是如何產生的。 – 2011-04-20 13:30:35
舉起!你其實是正確的。在我的測試中,我改變了原來的賦值和調用,因爲我不知道deque((a,b))不等於some_deque.append((a,b)) – 2011-04-20 13:33:35