where function

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

    #1

    where function

    Is there a function in Python analogous to the "where" function in
    IDL?

    x=[0,1,2,3,4,2,8,9]
    print where(x=2)

    output:
    [2,5]

  • bearophileHUGS@lycos.com

    #2
    Re: where function

    vorticitywo:
    Is there a function in Python analogous to the "where" function in
    IDL?
    Python gives very elastic syntax, you can simply do:

    data = [0,1,2,3,4,2,8,9]
    print [pos for pos, el in enumerate(data) if el==2]

    Bye,
    bearophile

    Comment

    • drochom

      #3
      Re: where function

      On 18 Mar, 15:19, vorticitywo...@ gmail.com wrote:
      Is there a function in Python analogous to the "where" function in
      IDL?
      >
      x=[0,1,2,3,4,2,8,9]
      print where(x=2)
      >
      output:
      [2,5]
      You can try this:

      print filter( lambda x: a[x]==2, range(len(a)))

      However it's not the best solution...

      Comment

      • vorticitywolfe@gmail.com

        #4
        Re: where function

        On Mar 18, 10:48 pm, bearophileH...@ lycos.com wrote:
        vorticitywo:
        >
        Is there a function in Python analogous to the "where" function in
        IDL?
        >
        Python gives very elastic syntax, you can simply do:
        >
        data = [0,1,2,3,4,2,8,9]
        print [pos for pos, el in enumerate(data) if el==2]
        >
        Bye,
        bearophile
        Thank you both, a little more cumbersome than I expected, but it does
        the job! Thanks!

        Comment

        • Shane Geiger

          #5
          Re: where function

          a =
          [0,1,2,3,4,2,8,9]



          # method
          1

          print [i for i in xrange(len(a)) if
          a[i]==2]



          def
          where(a,val):

          return [i for i in xrange(len(a)) if
          a[i]==val]



          # method
          2

          print
          where(a,2)







          vorticitywolfe@ gmail.com wrote:
          On Mar 18, 10:48 pm, bearophileH...@ lycos.com wrote:
          >
          >vorticitywo:
          >>
          >>
          >>Is there a function in Python analogous to the "where" function in
          >>IDL?
          >>>
          >Python gives very elastic syntax, you can simply do:
          >>
          >data = [0,1,2,3,4,2,8,9]
          >print [pos for pos, el in enumerate(data) if el==2]
          >>
          >Bye,
          >bearophile
          >>
          >
          Thank you both, a little more cumbersome than I expected, but it does
          the job! Thanks!
          >
          >
          --
          Shane Geiger
          IT Director
          National Council on Economic Education
          sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

          Leading the Campaign for Economic and Financial Literacy


          Comment

          • Steven Bethard

            #6
            Re: where function

            vorticitywolfe@ gmail.com wrote:
            Is there a function in Python analogous to the "where" function in
            IDL?
            >
            x=[0,1,2,3,4,2,8,9]
            print where(x=2)
            >
            output:
            [2,5]
            If you're doing a lot of this kind of thing, you probably want to use
            numpy::
            >>import numpy
            >>x = numpy.array([0, 1, 2, 3, 4, 2, 8, 9])
            >>numpy.where (x == 2)
            (array([2, 5]),)

            You can find numpy here:



            STeVe

            Comment

            • Scott David Daniels

              #7
              Re: where function

              vorticitywolfe@ gmail.com wrote:
              On Mar 18, 10:48 pm, bearophileH...@ lycos.com wrote:
              >[...fine solutions to the problem as asked...]
              Thank you both, a little more cumbersome than I expected, but it does
              the job! Thanks!
              The obvious simple near-equivalent is:

              data = range(33,99)
              print data.index(45)

              And "generalize d":
              data = [x % 9 for x in range(30)]
              result = []
              former = -1
              try:
              while True:
              former = data.index(3, former + 1)
              result.append(f ormer)
              except ValueError:
              print result
              else:
              print 'Nothing found'

              --
              --Scott David Daniels
              scott.daniels@a cm.org

              Comment

              Working...