作者veriaw (ver)
看板Python
标题[问题] node, actions, visited=fringe.pop()
时间Wed Mar 23 11:48:19 2016
不好意思怕有人误会
得先说明这是作业,但没要求解释以下的问题
最近在写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://webptt.com/cn.aspx?n=bbs/Python/M.1458704903.A.DB6.html
※ 编辑: veriaw (140.112.49.174), 03/23/2016 12:22:29
1F:→ uranusjr: 这三个其实是同一个概念: tuple, 第一个叫做 unpacking 03/23 12:37
2F:→ uranusjr: on assignment, 就是直接把回传的 tuple 在 = 时展开 03/23 12:38
3F:→ uranusjr: 在 for 回圈放多个变数也是这个概念的变形 03/23 12:38
4F:推 caim0725: 每迭代一次就会产生三个元素 再一一指派给左侧 03/23 23:20
5F:推 ResolaQQ: 2的理解如果我没误会的话,是错误的 03/24 02:36
6F:→ ResolaQQ: 白话点,1的意思是 03/24 02:37
7F:→ ResolaQQ: 写 a, b, c = (1, 2, 3),则 a = 1, b = 2, c = 3 03/24 02:38
8F:→ ResolaQQ: 2我猜你以为是 03/24 02:39
9F:→ ResolaQQ: list.append(a), list.append(b), list.append(c) 03/24 02:40
10F:→ ResolaQQ: 但实际上是 list.append( (a, b, c) ) 03/24 02:40
11F:→ ResolaQQ: 前面是加入三个物件,後面是加入一个物件但它有三个资料 03/24 02:41
12F:→ ResolaQQ: 3同1,把i代换成1的例子就对了 03/24 02:42
13F:→ veriaw: 非常感谢3位的协助 03/26 23:13
14F:→ veriaw: 我的误解完全解开了~~ 03/26 23:19