thing.f
and thing['f']
do different things. Use
self.openlist.sort(key=operator.attrgetter('f'))
Question
Edit
I've tried
self.openlist.sort(key = lambda d: (d['f']))
and
import operator
self.openlist.sort(key = operator.itemgetter('f'))
and both crash with AttributeError: Node instance has no attribute 'getitem'
End Edit
It may be due to my general unfamiliarity with Python, but I wasn't able to find an answer to my question via searching, so I am asking here.
I am writing an A* program to find the shortest path between cities.
I am storing all of the cities I can travel to at each step in a list, self.openlist, and as I process them I am assigning them all subvalues, such as self.openlist.g, self.openlist.h, self.openlist.f, and others.
I need to sort self.openlist by the f subvalue, from lowest to highest.
Also, I have a lot of unnecessary print's in there just to help me keep track of where the program was while I was debugging it.
Any tips would be appreciated. Below is my code:
import adata
class AS:
def __init__(self, startcity, goalcity, tracefunc):
self.startcity = startcity
self.goalcity = goalcity
self.tracefunc = tracefunc
self.openlist = [Node([startcity])]
self.closedlist = []
self.openlist[0].name = startcity
self.openlist[0].path = startcity
def astar_run(self, dbg=""):
while self.openlist:
if self.openlist[0].name == self.goalcity:
print self.openlist[0].path
return self.openlist[0]
else:
print "Current city", self.openlist[0].name
c2 = Roads(self.openlist[0].name)
templist = []
print "c2 contains", c2
print "Templist is", templist
x = 1
print "Length of c2", len(c2)
for i in range(0, len(c2)):
print "Test point", x
print "i value is now:", i
print c2[i]
templist.append(Node(c2[i]))
templist[i].name = c2[i]
templist[i].g = Distance(self.openlist[0].name, c2[i]) + self.openlist[0].g
templist[i].h = Distance(c2[i], self.goalcity)
templist[i].f = templist[i].g + templist[i].h
templist[i].path = self.openlist[0].path + ", to " + c2[i]
self.openlist.append(templist[i])
print "x value is now:", x
x = x + 1
print self.openlist[i].name
self.closedlist.append(self.openlist[0].name)
del self.openlist[0]
p = 0
q = len(self.openlist)
while p in range(0, q):
print "Openlist", p, "is", self.openlist[p].name
if self.openlist[p].name in self.closedlist:
print "Deleting", self.openlist[p]
del self.openlist[p]
p = p - 1
q = len(self.openlist)
p = p + 1
#print "Openlist", p, "is now", self.openlist[p].name
print "Closedlist is", self.closedlist
def Roads(city):
c1 = adata.roadlist(city)
return c1
def Distance(city1, city2):
c3 = adata.dist(city1, city2)
return c3
class Node:
def __init__(self, path=[], f=0, g=0, h=0):
self.path = path[:]
self.name = []
self.f = f
self.g = g
self.h = h
Also, I realize that my tabs on the def's have been lost transposing the code to this site, I apologies. However, all of them are properly tabbed in my py.
Thanks in advance!
Solution
thing.f
and thing['f']
do different things. Use
self.openlist.sort(key=operator.attrgetter('f'))