[Tutor] Linked List

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Mar 7 02:48:04 CET 2005



> > >>> myls=range(50)
> > >>> for i in myls:
> >         print i
> >         if i==20:
> >                 myls.insert(5,5)
> >
> > The point is, the list(both size and elements) is
> > changing even as it is being operated upon.
>
> My first thought was to say, "Use a queue.Queue."  But it appears that
> you need to be able to do more than just add items at the end of the
> queue.
>
> I suspect that what you need is along the lines of a "priority queue".
>  That is, something that works approximately like a queue, but when you
> add items to it, you also specify a priority for them.  Then, when you
> retrieve an item from the queue, what you get is not necessarily the
> first-inserted item, but rather the item with highest priority.


Priority queues are already a part of the Standard Library in the 'heapq'
module:

    http://www.python.org/doc/lib/module-heapq.html

We used this as part of a program that showed an example of a discrete
simulation:

    http://aspn.activestate.com/ASPN/Mail/Message/python-tutor/2503815


Best of wishes!



More information about the Tutor mailing list