Finding the array index number for known content.

Josiah Carlson jcarlson at uci.edu
Fri Oct 29 21:47:20 EDT 2004


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