concise code (beginner)

bambam david at asdf.asdf
Mon Sep 10 03:21:58 CEST 2007


> Removing from a list while you iterate will had quadratic performance

Anecdote:
    I was doing a route-finding program for a railway
ticketing system. My replacement explained to my boss
that it couldn't be done: the problem was one of that
class of problems that has no good optimum solution.
My replacement told me this while we were doing the
hand-over. I told him that the route-finding optimiser
was already complete.

It did a search of possible routes over the 3 loops, 15
branches and 75 stations in less time than it took to
redraw the screen, and that was without even bothering
to recode the tail recursion.

In my present case, the lists I am working with have ~10
elements.  Defined behaviour, even if it was inapropriate
for my needs, would be welcome. Language enhancement
that make the  code clearer and easier would be welcome.
Optimisers for large sets I could continue to do by other means.

Raw cPython is probably not a good choice for real-time
signal processing, but that's not what I am doing.

> O(n) to find the element you wish to remove and move over
> everything after it,

Is that how lists are stored in cPython? It seems unlikely?

Steve.

"Rhamphoryncus" <rhamph at gmail.com> wrote in message 
news:1189183560.572177.56930 at r34g2000hsd.googlegroups.com...
> On Sep 6, 1:56 pm, Karthik Gurusamy <kar1... at gmail.com> wrote:
>> That said, it may be a good future language enhancement to define a
>> reasonable consistent behavior for an iterator over a changing
>> collection. This occurs quite common when we walk a collection and
>> usually delete the current item.
>>





More information about the Python-list mailing list