Determinig position of a element in a list

Bob Gailer bgailer at alum.rpi.edu
Mon Aug 25 18:58:34 EDT 2003


At 12:29 AM 8/26/2003 +0200, Przemo Drochomirecki wrote:

>Hello,
>i'm wondering if there is any tricky way for doing following thing:
>     A - list of distinct integers  (e.x. A = [1,3,7,11,14,15])
>     very fast function determinig position of number x in list A
>     or -1 if x doesnt belong to A
>Operator IN returns only false/true values
>i can implement function index (e.x. index(5,A) = -1, index(7,A) = 2), but
>maybe there's is simpler(builtin?) solution

try: pos = A.index(5) # or whatever number you want
except ValueError: pos = -1

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003


More information about the Python-list mailing list