get array element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan.Fodness@gmail.com

    get array element

    I have an array, and I would like to get the indice value.

    a = array([13,14,15,16])

    I would like something like a.getindice(15)

    If I want 15 it would return 2
  • Ivan Illarionov

    #2
    Re: get array element

    On Apr 10, 9:22 pm, "Bryan.Fodn...@ gmail.com"
    <Bryan.Fodn...@ gmail.comwrote:
    I have an array, and I would like to get the indice value.
    >
    a = array([13,14,15,16])
    >
    I would like something like a.getindice(15)
    >
    If I want 15 it would return 2
    >>a = array('i', [13,14,15,16])
    >>a.index(15)
    2

    Comment

    • Jason Scheirer

      #3
      Re: get array element

      On Apr 10, 10:22 am, "Bryan.Fodn...@ gmail.com"
      <Bryan.Fodn...@ gmail.comwrote:
      I have an array, and I would like to get the indice value.
      >
      a = array([13,14,15,16])
      >
      I would like something like a.getindice(15)
      >
      If I want 15 it would return 2
      a.index(15)

      Comment

      • Robert Kern

        #4
        Re: get array element

        Bryan.Fodness@g mail.com wrote:
        I have an array, and I would like to get the indice value.
        >
        a = array([13,14,15,16])
        >
        I would like something like a.getindice(15)
        >
        If I want 15 it would return 2
        You will want to ask numpy questions on the numpy mailing list. If you don't
        mention that you are using numpy here, people get confused.



        Anyways, if your array is sorted, use a.searchsorted( 15). If it isn't sorted,
        then you can find all of the indices equal to the value with the following:

        In [7]: from numpy import *

        In [8]: a = array([13,14,15,16])

        In [9]: nonzero(a == 15)[0]
        Out[9]: array([2])

        --
        Robert Kern

        "I have come to believe that the whole world is an enigma, a harmless enigma
        that is made terrible by our own mad attempt to interpret it as though it had
        an underlying truth."
        -- Umberto Eco

        Comment

        Working...