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: Kay Schluehr <kay.schluehr@gmx.net>
Date: Sun Apr 30 2006 - 09:38:24 CEST

> * 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.

Hi Steven,

your solution may not create the correct answer if an item occurs twice
in the list because the later occurrence overwrites the former during
dict creation:

>>> L = ['C', 'A', 'D', 'B', 'A']
>>> dict((item, i) for i, item in enumerate(L))
{'A': 4, 'C': 0, 'B': 3, 'D': 2}

This gives the impression that 'D' always precedes 'A' which is wrong.
Received on Mon May 1 00:46:44 2006