Finding the array index number for known content.

PhilC pcooke at philc.net
Sat Oct 30 04:04:12 CEST 2004


That was quick :)

Thank you very much indeed.

Regards

Phil

On Fri, 29 Oct 2004 18:47:20 -0700, Josiah Carlson <jcarlson at uci.edu>
wrote:

>
>PhilC <pcooke at philc.net> wrote:
>> If I have an array holding a pair of numbers, and that pairing is
>> unique, is there a way that I can find the array index number for that
>> pair?
>
>If by "array" you mean "Python list", then yes:
>   lst.index(pair)
>
>If by "array" you mean "array from the array module", then "pair of
>numbers" is just two adjacent numbers that are of your 'pair' value,
>then you are going to have to do it by hand...
>
>def find_pair(arry, val1, val2):
>    for i in xrange(len(arry)-1):
>        if arry[i] == val1 and arry[i+1] == val2:
>            return i
>    raise IndexError, "pair (%s, %s) not found in array"%(val1, val2)
>
>
> - Josiah




More information about the Python-list mailing list