Python字典键错误';0';但是0在字典中
在graph类中,我制作了一个存储顶点名称和顶点类对象的vertList字典,该字典在调用Dijkstra函数时在relax函数中抛出关键错误0 我尝试使用get函数,而不是直接从字典本身调用Python字典键错误';0';但是0在字典中,python,python-3.x,dictionary,graph,dijkstra,Python,Python 3.x,Dictionary,Graph,Dijkstra,在graph类中,我制作了一个存储顶点名称和顶点类对象的vertList字典,该字典在调用Dijkstra函数时在relax函数中抛出关键错误0 我尝试使用get函数,而不是直接从字典本身调用 from collections import defaultdict import math import heapq class PriorityQueue: def __init__(self): self.is_empty = True self.len
from collections import defaultdict
import math
import heapq
class PriorityQueue:
def __init__(self):
self.is_empty = True
self.length = 0
self.pqueue = []
def makePQueue(self, l):
self.pqueue = l.copy()
heapq.heapify(self.pqueue)
if(len(l) > 0):
self.length = len(l)
self.is_empty = False
def addElement(self, element):
heapq.heappush(self.pqueue, element)
self.length = self.length+1
def removeElement(self):
if(self.length > 0):
element = heapq.heappop(self.pqueue)
self.length = self.length-1
if(self.length == 0):
self.is_empty = True
return element
else:
return None
# vertex class
class Vertex:
def __init__(self, key):
self.id = key
self.parent = None
self.distFromSource = math.inf
def getId(self):
return self.id
def getParent(self):
return self.parent
def addParent(self, p):
self.parent = p
def getDistSource(self):
return self.distFromSource
def setDistFromSource(self, d):
self.distFromSource = d
# Graph class
class Graph:
def __init__(self):
self.graph = defaultdict(list)
self.soruce = None
self.vertList = {}
def addVertex(self, v):
if(v not in list(self.vertList.keys())):
ver = Vertex(v)
self.vertList[v] = ver
def addEdge(self, u, v, c):
if(u not in list(self.vertList.keys())):
ver = Vertex(u)
self.vertList[u] = ver
if(v not in list(self.vertList.keys())):
ver = Vertex(v)
self.vertList[v] = ver
self.graph[u].append((v, c))
def getWeight(self, u, v):
# binary search can be implemented for speed
for i in self.graph[u]:
if(i[0] == v):
return i[1]
def setSource(self, s):
if(s not in list(self.vertList.keys())):
ver = Vertex(s)
self.vertList[s] = ver
self.vertList[s].setDistFromSource(0)
self.source = self.vertList[s]
self.source.setDistFromSource(0)
def getSource(self):
return self.source.getId()
def getDistList(self):
l = [(self.vertList[i].getDistSource(), str(i))
for i in list(self.vertList.keys())]
return l
# def costArray(self):
# l = [i.]
# implementation of edge array of cost
def relax(self, u, v, c):
if(self.vertList[v].getDistSource() > self.vertList[u].getDistSource()+c):
self.vertList[v].setDistFromSource(
self.vertList[u].getDistSource()+c)
self.vertList[v].addParent(self.vertList[u])
def dijkstra(graph):
ss = []
l = graph.getDistList()
pq = PriorityQueue()
pq.makePQueue(l)
# print(pq.pqueue)
while(pq.is_empty == False):
(cost, u) = pq.removeElement()
# in priority queue on the basis of cost
if int(u) not in ss:
ss = ss.append(int(u))
for (i, c) in graph.graph[int(u)]:
graph.relax(u, i, c)
```
g = Graph()
g.addEdge(0, 1, 3)
g.addEdge(0, 2, 2)
g.addEdge(0, 3, 5)
g.addEdge(1, 0, 3)
g.addEdge(1, 4, 3)
g.addEdge(4, 1, 3)
g.addEdge(4, 2, 1)
g.addEdge(4, 6, 4)
g.addEdge(2, 0, 2)
g.addEdge(2, 4, 1)
g.addEdge(2, 5, 6)
g.addEdge(3, 0, 5)
g.addEdge(3, 5, 2)
g.addEdge(5, 2, 6)
g.addEdge(5, 3, 2)
g.addEdge(5, 6, 1)
g.addEdge(5, 7, 4)
g.addEdge(6, 4, 4)
g.addEdge(6, 5, 1)
g.addEdge(6, 7, 2)
g.addEdge(7, 5, 4)
g.addEdge(7, 6, 2)
g.setSource(0)
dijkstra(g)
例外情况
python graph.py
Traceback (most recent call last):
File "graph.py", line 155, in <module>
dijkstra(g)
File "graph.py", line 126, in dijkstra
graph.relax(u, i, c)
File "graph.py", line 108, in relax
if(self.vertList[v].getDistSource() >
self.vertList[u].getDistSource()+c):
KeyError: '0'
python-graph.py
回溯(最近一次呼叫最后一次):
文件“graph.py”,第155行,在
迪克斯特拉(g)
dijkstra文件“graph.py”,第126行
图.松弛(u,i,c)
relax中第108行的文件“graph.py”
if(self.vertList[v].getDistSource()>
self.vertList[u].getDistSource()+c):
KeyError:“0”
调用relax()
方法时,u
是字符串,而不是整数
这就是为什么会出现键错误:确实存在0
键,但不是'0'
定义优先级队列时,显式存储字符串:
def getDistList(self):
l = [(self.vertList[i].getDistSource(), str(i))
for i in list(self.vertList.keys())]
return l
然后,在dijkstra
方法中,将该字符串转换为if
语句中的int,但不在graph.relax()调用之后:
if int(u) not in ss:
ss = ss.append(int(u))
for (i, c) in graph.graph[int(u)]:
graph.relax(u, i, c)
在进行任何进一步调试之前,我将检查类图的属性soruce,我认为它是source,并且您键入了错误,因为类GraphgetSource中定义的函数返回self.source.getId()很明显,0
不同于'0'
?(首先尝试调试代码,看看哪个变量等于'0'
,该值从何处传播。)我不认为它应该被视为重复,因为主要的问题是找到为什么u
参数是字符串而不是int…是的,这是正确的,但不会影响错误是的,这解决了我的问题谢谢