[問題] node, actions, visited=fringe.pop()

看板Python作者 (ver)時間8年前 (2016/03/23 11:48), 8年前編輯推噓2(2012)
留言14則, 4人參與, 最新討論串1/1
不好意思怕有人誤會 得先說明這是作業,但沒要求解釋以下的問題 最近在寫berkelyey pacman 有3個步驟有點不太明白 想請問 (1) node, actions, visited = fringe.pop() 一次把右邊的pop assign給左邊的多個variables是什麼意思呢? (2) fringe.push((coord, actions+[direction], visited+[node])) push定義如下,我的理解是依序把coord, actions+[direction], visited+[node] 加入到list中,不知是否正確呢? (3) for coord, direction, steps in problem.getSuccessors(node): 我的程度只到 for i in list: 有點不太明白for 後面接多個variables跑的意思是什麼 #Code: def depthFirstSearch(problem) fringe = util.Stack() fringe.push( (problem.getStartState(), [], []) ) while not fringe.isEmpty(): node, actions, visited = fringe.pop() for coord, direction, steps in problem.getSuccessors(node): if not coord in visited: if problem.isGoalState(coord): return actions + [direction] fringe.push((coord, actions + [direction], visited + [node] )) return [] #util class Stack: "A container with a last-in-first-out (LIFO) queuing policy." def __init__(self): self.list = [] def push(self,item): "Push 'item' onto the stack" self.list.append(item) def pop(self): "Pop the most recently pushed item from the stack" return self.list.pop() def isEmpty(self): "Returns true if the stack is empty" return len(self.list) == 0 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.100 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1458704903.A.DB6.html ※ 編輯: veriaw (140.112.49.174), 03/23/2016 12:22:29

03/23 12:37, , 1F
這三個其實是同一個概念: tuple, 第一個叫做 unpacking
03/23 12:37, 1F

03/23 12:38, , 2F
on assignment, 就是直接把回傳的 tuple 在 = 時展開
03/23 12:38, 2F

03/23 12:38, , 3F
在 for 迴圈放多個變數也是這個概念的變形
03/23 12:38, 3F

03/23 23:20, , 4F
每迭代一次就會產生三個元素 再一一指派給左側
03/23 23:20, 4F

03/24 02:36, , 5F
2的理解如果我沒誤會的話,是錯誤的
03/24 02:36, 5F

03/24 02:37, , 6F
白話點,1的意思是
03/24 02:37, 6F

03/24 02:38, , 7F
寫 a, b, c = (1, 2, 3),則 a = 1, b = 2, c = 3
03/24 02:38, 7F

03/24 02:39, , 8F
2我猜你以為是
03/24 02:39, 8F

03/24 02:40, , 9F
list.append(a), list.append(b), list.append(c)
03/24 02:40, 9F

03/24 02:40, , 10F
但實際上是 list.append( (a, b, c) )
03/24 02:40, 10F

03/24 02:41, , 11F
前面是加入三個物件,後面是加入一個物件但它有三個資料
03/24 02:41, 11F

03/24 02:42, , 12F
3同1,把i代換成1的例子就對了
03/24 02:42, 12F

03/26 23:13, , 13F
非常感謝3位的協助
03/26 23:13, 13F

03/26 23:19, , 14F
我的誤解完全解開了~~
03/26 23:19, 14F
文章代碼(AID): #1MyX87ss (Python)