Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64413
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: which data structure to use? |
| Date | 2014-01-21 15:09 +0100 |
| Organization | None |
| References | <a0d3734d-5275-4b28-9a3f-6957474674d7@googlegroups.com> <mailman.5780.1390305003.18130.python-list@python.org> <cfa3e258-1f7d-4c08-81d5-41d9c84d40ac@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5784.1390313352.18130.python-list@python.org> (permalink) |
Robert Voigtländer wrote:
>
>> On Tue, Jan 21, 2014 at 03:17:43AM -0800, Robert Voigtl�nder wrote:
>>
>
>> > I have objects like this:
>>
>> >
>>
>> > class Node(object):
>>
>> > def __init__(self, pos, parent, g , h):
>>
>> > self.pos = pos
>>
>> > self.parent = parent
>>
>> > self.g = g
>>
>> > self.h = h
>>
>> > self.f = g+h
>>
>> >
>>
>> >
>>
>> > I need to build a "list" of these objects. The amount is unknown.
>>
>> > On this list I need to regularly
>>
>> >
>>
>> > 1. check if a specific item - identified by Node.pos - is in the list.
>>
>> > 2. find the object with the lowest Node.f attribute and update or
>> > remove it
>>
>
>
> First thanks to all who responded. Although I only partially understand
> your answers as I am a Python starter. What's clear to me is the
> difference between object and instance of an object. Just didn't explain
> it well.
>
> Maybe I give some more info on what I need / want to do. I will also
> provide a working code example. Should have done this before.
>
> I would very much appreciate a working example of what you mean. Then I
> have a chance to understand it. :-)
>
> I would like to implement a class for a A* pathfinding algorithm. (there
> are ready libraries out there but I would like to learn it myself) This
> requires to maintain a list of nodes to be processed and nodes already
> processed. For new nodes I need to check if they are on one of the lists.
> I also need to regularly pick the node with the lowest value f from the
> list.
>
> Here some working code. For one function I sill need a solution. Any
> better solution is welcome.
>
> Thanks
> Robert
>
> ---------------
> class Node:
> def __init__(self, pos, parent, g , h):
> self.pos = pos
> self.parent = parent
> self.g = g
> self.h = h
> self.f = g+h
>
>
> def isinlist(nodeToSeatch):
> for item in openlist:
> if item.pos == nodeToSeatch: return True
> return False
>
>
> def lowestF():
> lowestF = ''
> for item in openlist:
> if item.f < lowestF: lowestF = item
> return lowestF
def lowestF():
return min(openlist, key=operator.attrgetter("f"))
> def deleteItemWithPos(pos):
> ## need this function or different approach
> pass
>
> openlist=[]
> openlist.append(Node((1,1),None,1,5))
> openlist.append(Node((1,2),(1,1),4,6))
> openlist.append(Node((1,3),(1,2),9,10))
>
> for item in openlist: print item.pos, item.f
>
> print isinlist((1,1))
> print isinlist((1,5))
>
> nextNode = lowestF()
> print nextNode.pos, nextNode.f
Here is an OO implementation of Chris Angelico's suggestion:
import heapq
class Node:
def __init__(self, pos, parent, g , h):
self.pos = pos
self.parent = parent
self.g = g
self.h = h
self.f = g+h
def __str__(self):
return "Node(pos={!r}, f={!r})".format(self.pos, self.f)
class Nodes():
def __init__(self):
self.lookup = {}
self.heap = []
def add(self, node):
self.lookup[node.pos] = node
heapq.heappush(self.heap, (node.f, node))
def __iter__(self):
return iter(self.lookup.values())
def __contains__(self, pos):
return pos in self.lookup
def lowest(self):
return self.heap[0][1]
def pop(self):
f, node = heapq.heappop()
del lookup[node.pos]
return node
nodes = Nodes()
nodes.add(Node((1,1), None, 1, 5))
nodes.add(Node((1,2), (1,1), 4, 6))
nodes.add(Node((1,3), (1,2), 9, 10))
for node in nodes:
print(node)
print((1,1) in nodes)
print((1,5) in nodes)
print(nodes.lowest())
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
which data structure to use? Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 03:17 -0800
Re: which data structure to use? Chris Angelico <rosuav@gmail.com> - 2014-01-21 22:27 +1100
Re: which data structure to use? Ben Finney <ben+python@benfinney.id.au> - 2014-01-21 22:34 +1100
Re: which data structure to use? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-01-21 11:49 +0000
Re: which data structure to use? Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 05:38 -0800
Re: which data structure to use? Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 05:43 -0800
Re: which data structure to use? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-21 18:03 +0000
Re: which data structure to use? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-01-21 13:59 +0000
Re: which data structure to use? Peter Otten <__peter__@web.de> - 2014-01-21 15:09 +0100
Re: which data structure to use? Peter Otten <__peter__@web.de> - 2014-01-21 15:19 +0100
Re: which data structure to use? Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 07:33 -0800
Re: which data structure to use? Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-21 07:37 -0800
Re: which data structure to use? Peter Otten <__peter__@web.de> - 2014-01-21 19:39 +0100
Re: which data structure to use? Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-01-22 01:38 -0800
csiph-web