![](/img/trans.png)
[英]Cloned list seems to be functioning as an alias, even though explicitly declared as a a clone
[英].pop() seems to be triggering a TypeError in Python, even though the result should be a list
所以下面的Python代码得到错误:TypeError:'NoneType'对象没有属性' getitem '
我无法弄清楚为什么列表'路径1 '不会被识别为列表,而是被识别为NoneType。
我已经检查了之前的Stack问题,谷歌搜索了所有这些,但我无法弄清楚它为什么会发生。 这是近距离的(我认为),但我不知道为什么我的state = path [-1]调用正在出现这个错误。
有什么想法吗? 非常感激。
谢谢
码:
import re
import string
gr = {
"A":["B","C","D"],
"B":["A","E"],
"C":["A","E"],
"D":["A","H","I","J"],
"E":["B","C","F","G","H"],
"F":["E"],
"G":["E","H"],
"H":["D","E","G","K"],
"I":["D","K","L"],
"J":["D","M"],
"K":["I","M","H"],
"L":["I"],
"M":["K","J"]
}
def graphSearch(graph, start, dest):
frontier = [[start]]
explored = []
options = []
if frontier == []: return "I hope you enjoyed riding aboard the failboat"
while len(frontier)>0:
path = frontier.pop()
state = path[-1]
if state == dest:
return path
else:
for a in graph[state]:
if a not in path:
newP = path.append(a)
frontier.append(newP)
return options
print graphSearch(gr, "A", "M")
newP = path.append(a)
frontier.append(newP)
append不会返回任何内容(它会修改原始列表),因此您最终会在列表中附加一堆None
。
.pop()
工作正常; 这是失败的state = path[1]
,因为pop()
弹出了你在上一次迭代中附加的None
项之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.