Re: best way to determine sequence ordering?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: best way to determine sequence ordering?

From: Justin Azoff <justin.azoff@gmail.com>
Date: Sun Apr 30 2006 - 01:47:34 CEST

John Salerno wrote:
> If I want to make a list of four items, e.g. L = ['C', 'A', 'D', 'B'],
> and then figure out if a certain element precedes another element, what
> would be the best way to do that?
>
> Looking at the built-in list functions, I thought I could do something like:
>
> if L.index('A') < L.index('D'):
> # do some stuff

This actually performs pretty well since list.index is implemented in
C.

The obvious (to me) implementation of:
def before(lst, a, b):
    for x in lst:
        if x == a:
            return True
        if x == b:
            return False

runs about 10-50 times faster than the double index method if I use
psyco. Without psyco, it ends up being faster for the cases where a or
b appears early on in the list, and the other appears towards the end.
Received on Mon May 1 00:45:49 2006