Simple question about Python lists

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

    Simple question about Python lists

    I'm learning Python (while coming from MATLAB). One question I have is
    that if I have a list with say 8 elements, and I want just a few of
    them how do I select them out. In MATLAB, if I just want the first,
    fifth and eighth element I might do something like this:

    b = a([1 5 8]);

    I can't seem to figure out a similar Python construct for selecting
    specific indices. Any suggestions?

    Thanks,

    Eric
  • Marc 'BlackJack' Rintsch

    #2
    Re: Simple question about Python lists

    On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote:
    I'm learning Python (while coming from MATLAB). One question I have is
    that if I have a list with say 8 elements, and I want just a few of them
    how do I select them out. In MATLAB, if I just want the first, fifth and
    eighth element I might do something like this:
    >
    b = a([1 5 8]);
    >
    I can't seem to figure out a similar Python construct for selecting
    specific indices. Any suggestions?
    b = [a[i] for i in [1, 5, 8]]

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Guilherme Polo

      #3
      Re: Simple question about Python lists

      On Tue, Nov 11, 2008 at 5:47 PM, Eric <eric.shain@gma il.comwrote:
      I'm learning Python (while coming from MATLAB). One question I have is
      that if I have a list with say 8 elements, and I want just a few of
      them how do I select them out. In MATLAB, if I just want the first,
      fifth and eighth element I might do something like this:
      >
      b = a([1 5 8]);
      >
      I can't seem to figure out a similar Python construct for selecting
      specific indices. Any suggestions?
      >
      MATLAB works with 1-based index, while Python is 0-based, so accessing
      the index number 8 wouldn't be valid with 8 elements here in Python.

      Now, to solve your problem you could either use the already suggested
      answer above mine or depending on what you what else you wanna do, you
      will feel more comfortable using numpy.

      Supposing you have a array with 8 elements: x = numpy.arange(8)
      To pull out the first, fifth and eighth elements you would do:
      x[numpy.array([0, 4, 7])]

      It is so no natural as it was in MATLAB, but well, it was a different language.


      --
      -- Guilherme H. Polo Goncalves

      Comment

      • Eric

        #4
        Re: Simple question about Python lists

        On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
        On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote:
        I'm learning Python (while coming from MATLAB). One question I have is
        that if I have a list with say 8 elements, and I want just a few of them
        how do I select them out. In MATLAB, if I just want the first, fifth and
        eighth element I might do something like this:
        >
        b = a([1 5 8]);
        >
        I can't seem to figure out a similar Python construct for selecting
        specific indices. Any suggestions?
        >
        b = [a[i] for i in [1, 5, 8]]
        >
        Ciao,
                Marc 'BlackJack' Rintsch
        Thanks! It makes sense, but in this case MATLAB seems easier and no
        less readable. That said, I know better than for a newbie like me to
        question syntax issues.

        Regards,

        Eric

        Comment

        • Robert Kern

          #5
          Re: Simple question about Python lists

          Guilherme Polo wrote:
          On Tue, Nov 11, 2008 at 5:47 PM, Eric <eric.shain@gma il.comwrote:
          >I'm learning Python (while coming from MATLAB). One question I have is
          >that if I have a list with say 8 elements, and I want just a few of
          >them how do I select them out. In MATLAB, if I just want the first,
          >fifth and eighth element I might do something like this:
          >>
          >b = a([1 5 8]);
          >>
          >I can't seem to figure out a similar Python construct for selecting
          >specific indices. Any suggestions?
          >
          MATLAB works with 1-based index, while Python is 0-based, so accessing
          the index number 8 wouldn't be valid with 8 elements here in Python.
          >
          Now, to solve your problem you could either use the already suggested
          answer above mine or depending on what you what else you wanna do, you
          will feel more comfortable using numpy.
          >
          Supposing you have a array with 8 elements: x = numpy.arange(8)
          To pull out the first, fifth and eighth elements you would do:
          x[numpy.array([0, 4, 7])]
          Actually, x[[0,4,7]] will work just as well.

          --
          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

          • Robert Kern

            #6
            Re: Simple question about Python lists

            Eric wrote:
            On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
            >On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote:
            >>I'm learning Python (while coming from MATLAB). One question I have is
            >>that if I have a list with say 8 elements, and I want just a few of them
            >>how do I select them out. In MATLAB, if I just want the first, fifth and
            >>eighth element I might do something like this:
            >>b = a([1 5 8]);
            >>I can't seem to figure out a similar Python construct for selecting
            >>specific indices. Any suggestions?
            >b = [a[i] for i in [1, 5, 8]]
            >>
            >Ciao,
            > Marc 'BlackJack' Rintsch
            >
            Thanks! It makes sense, but in this case MATLAB seems easier and no
            less readable. That said, I know better than for a newbie like me to
            question syntax issues.
            In my experience, I never do this with lists so there's no optimized syntax for
            it. I do use it very often with numpy arrays, and that does have optimized syntax.

            --
            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

            • Scott David Daniels

              #7
              Re: Simple question about Python lists

              Eric wrote:
              ... In MATLAB, if I just want the first, fifth and eighth element I
              might do something like this: b = a([1 5 8]);
              On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net replied:
              >b = [a[i] for i in [1, 5, 8]]
              To which Eric said:
              Thanks! It makes sense, but in this case MATLAB seems easier and no
              less readable. That said, I know better than for a newbie like me to
              question syntax issues.
              If you are using numpy, you may find found the following link useful:



              --Scott David Daniels
              Scott.Daniels@A cm.Org

              Comment

              Working...