Numeric array equivalent of index ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Pernetty

    #1

    Numeric array equivalent of index ?

    Hello,

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

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


    Does someone know of a more Pythonic way ?

    Thanks,
  • Robert Kern

    #2
    Re: Numeric array equivalent of index ?

    Nicolas Pernetty wrote:[color=blue]
    > Hello,
    >
    > I'm trying to find a clear and fast equivalent to the index method of
    > plain python list :
    >[color=green][color=darkred]
    >>>a = [5,1,4,3,4]
    >>>a.index(4)[/color][/color]
    >
    > 2
    >
    > I have to use it on a Numeric array, so the best I've come up with is
    > (rather ugly I think) :
    >[color=green][color=darkred]
    >>>from Numeric import array, equal, nonzero
    >>>a = array([5,1,4,3,4])
    >>>nonzero(equa l(a,4))[0][/color][/color]
    >
    > 2
    >
    > Does someone know of a more Pythonic way ?[/color]

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



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

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    Working...