best way to determine sequence ordering?

Steven Bethard steven.bethard at gmail.com
Sat Apr 29 20:37:20 EDT 2006


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

Ok, since, as happens some times, the discussion here has gotten 
extremely sidetracked from the original question, as a public service ;) 
I thought I'd give a brief summary of the options discussed and when 
they might be useful:

* The original::

       if L.index('A') < L.index('D'):
           # do some stuff

   If you're doing exactly one comparison, this is probably your best 
bet in most cases as .index() is coded in C.  It's clear, concise and 
correct.

* Justin Azoff's before()::

       def before(lst, a, b):
           for x in lst:
               if x == a:
                   return True
               if x == b:
                   return False

       if before(L, 'A', 'D'):
          # do some stuff

   You might gain a bit from this version if one of the elements you're 
looking for appears early in the list.  It only iterates through the 
list once, but performs two comparisons each time, so you'll only gain 
from it if you comparisons aren't too costly (e.g. you're comparing 
single character strings).

* building a dict of indicies::

       positions = dict((item, i) for i, item in enumerate(L))

       if positions['A'] < positions['D']:
           # do some stuff

   You'll only get a gain from this version if you need to do several 
comparisons instead of just one.


And most importantly, as Edward Elliot put it:

     Remember kids:
     1. Numbers can show anything
     2. Know your data set
     3. Premature optimizations are evil


HTH,

STeVe



More information about the Python-list mailing list