Numeric array equivalent of index ?

Discussion in 'Python' started by Nicolas Pernetty, Oct 15, 2005.

  1. Hello,

    I'm trying to find a clear and fast equivalent to the index method of
    plain python list :
    >> a = [5,1,4,3,4]
    >> a.index(4)

    2

    I have to use it on a Numeric array, so the best I've come up with is
    (rather ugly I think) :
    >> from Numeric import array, equal, nonzero
    >> a = array([5,1,4,3,4])
    >> nonzero(equal(a,4))[0]

    2


    Does someone know of a more Pythonic way ?

    Thanks,
    Nicolas Pernetty, Oct 15, 2005
    #1
    1. Advertising

  2. Nicolas Pernetty

    Robert Kern Guest

    Nicolas Pernetty wrote:
    > Hello,
    >
    > I'm trying to find a clear and fast equivalent to the index method of
    > plain python list :
    >
    >>>a = [5,1,4,3,4]
    >>>a.index(4)

    >
    > 2
    >
    > I have to use it on a Numeric array, so the best I've come up with is
    > (rather ugly I think) :
    >
    >>>from Numeric import array, equal, nonzero
    >>>a = array([5,1,4,3,4])
    >>>nonzero(equal(a,4))[0]

    >
    > 2
    >
    > Does someone know of a more Pythonic way ?


    There's a list for Numeric. You'll get better answers there.

    http://numeric.scipy.org/

    And no, no one has bothered adding a .index() equivalent for general,
    unsorted arrays.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Oct 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    911
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    788
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    932
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. Shawn W_
    Replies:
    5
    Views:
    262
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    267
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page