Finding the array index number for known content.

Skip Montanaro skip at pobox.com
Fri Oct 29 23:46:19 EDT 2004


    Phil> If I have an array holding a pair of numbers, and that pairing is
    Phil> unique, is there a way that I can find the array index number for
    Phil> that pair?

>From the "teach them to fish" department...

Python, being the introspective language that it is, helps you answer these
sorts of questions pretty easily.  Your question was in general, "is there a
way to do X with a list?".  The first step is to ask a list what it knows
about itself:

    % python
    Python 2.4b1 (#52, Oct 17 2004, 13:54:51) 
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> dir([])
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
    '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
    '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
    '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__',
    '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
    '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
    '__setslice__', '__str__', 'append', 'count', 'extend', 'index',
    'insert', 'pop', 'remove', 'reverse', 'sort'] 

Hmmm... there's an "index" attribute.  Let's see what that's all about:

    >>> help([].index)
    Help on built-in function index:

    index(...)
        L.index(value, [start, [stop]]) -> integer -- return first index of
        value

Looks promising (as Josiah already suggested it would be).

It's often also helpful to ask for help about an object's type (or class).
List objects are instances of the list type, and there is useful help about
it:

    >>> help(list)

    Help on class list in module __builtin__:

    class list(object)
     |  list() -> new list
     |  list(sequence) -> new list initialized from sequence's items
     |  
     |  Methods defined here:
     |  
     |  __add__(...)
     |      x.__add__(y) <==> x+y
     |  
     |  __contains__(...)
     |      x.__contains__(y) <==> y in x
     |  
     |  __delitem__(...)
     |      x.__delitem__(y) <==> del x[y]
     ...

The powers of the help builtin are available via the pydoc command as well,
e.g.: 

    % pydoc sys
    Help on built-in module sys:

    NAME
        sys

    FILE
        (built-in)

    MODULE DOCS
        http://www.python.org/doc/current/lib/module-sys.html

    DESCRIPTION
        This module provides access to some objects used or maintained by
        the interpreter and to functions that interact strongly with the
        interpreter.
    ...

    % pydoc list
    Help on class list in module __builtin__:

    class list(object)
     |  list() -> new list
     |  list(sequence) -> new list initialized from sequence's items
     |  
    ...

Pydoc even supports a web server interface.  Try something like:

    pydoc -p 8709

then visit

    http://localhost:8709/

To find out about builtin types (and builtin objects in general), click the
__builtin__ link.  If you want help on a module the help displayed will
likely have a link to the relevant section of the standard documentation as
well.

Now go fish my son.  Don't come back until you have caught your limit of
rainbow trout...

Skip



More information about the Python-list mailing list