Partially unpacking a sequence

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

    #1

    Partially unpacking a sequence

    I have a list y[color=blue][color=green][color=darkred]
    >>>y[/color][/color][/color]
    ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
    '19.500', '224']

    from which I want to extract only the 2nd and 4th item by partially
    unpacking the list. So I tried[color=blue][color=green][color=darkred]
    >>>a,b = y[2,4][/color][/color][/color]
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: list indices must be integers

    Out of curiosity, I tried[color=blue][color=green][color=darkred]
    >>>a,b = y[2:4]
    >>>a[/color][/color][/color]
    '04276410'[color=blue][color=green][color=darkred]
    >>> b[/color][/color][/color]
    '18.500'

    Why does this work (to a point - it gives me items 2 and 3, not 2 and
    4 as I require) and not my first attempt? What is the right syntax to
    use when partially upacking a sequence?

    Thanks in advance

    Thomas Philips

  • Paul McGuire

    #2
    Re: Partially unpacking a sequence

    <tkpmep@hotmail .com> wrote in message
    news:1144352710 .190839.165580@ z34g2000cwc.goo glegroups.com.. .[color=blue]
    > I have a list y[color=green][color=darkred]
    > >>>y[/color][/color]
    > ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
    > '19.500', '224']
    >
    > from which I want to extract only the 2nd and 4th item by partially
    > unpacking the list. So I tried[color=green][color=darkred]
    > >>>a,b = y[2,4][/color][/color]
    > Traceback (most recent call last):
    > File "<interacti ve input>", line 1, in ?
    > TypeError: list indices must be integers
    >
    > Out of curiosity, I tried[color=green][color=darkred]
    > >>>a,b = y[2:4]
    > >>>a[/color][/color]
    > '04276410'[color=green][color=darkred]
    > >>> b[/color][/color]
    > '18.500'
    >
    > Why does this work (to a point - it gives me items 2 and 3, not 2 and
    > 4 as I require) and not my first attempt? What is the right syntax to
    > use when partially upacking a sequence?
    >
    > Thanks in advance
    >
    > Thomas Philips
    >[/color]

    a,b = y[2],y[4]

    or

    a,b = y[2:5:2]

    or

    a,b = ( y[i] for i in (2,4) )


    -- Paul


    Comment

    • Fredrik Lundh

      #3
      Re: Partially unpacking a sequence

      tkpmep@hotmail. com wrote:
      [color=blue]
      > I have a list y[color=green][color=darkred]
      > >>>y[/color][/color]
      > ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
      > '19.500', '224']
      >
      > from which I want to extract only the 2nd and 4th item by partially
      > unpacking the list. So I tried[color=green][color=darkred]
      > >>>a,b = y[2,4][/color][/color]
      > Traceback (most recent call last):
      > File "<interacti ve input>", line 1, in ?
      > TypeError: list indices must be integers
      >
      > Out of curiosity, I tried[color=green][color=darkred]
      > >>>a,b = y[2:4]
      > >>>a[/color][/color]
      > '04276410'[color=green][color=darkred]
      > >>> b[/color][/color]
      > '18.500'
      >
      > Why does this work (to a point - it gives me items 2 and 3, not 2 and
      > 4 as I require) and not my first attempt? What is the right syntax to
      > use when partially upacking a sequence?[/color]

      if you want two items, fetch two items:

      a = y[2]
      b = y[4]

      y[2:4] is a 2-item slice starting at index 2 and ending *before* index 4.
      see the documentation for more on slicing.

      </F>



      Comment

      • Paul McGuire

        #4
        Re: Partially unpacking a sequence

        "Paul McGuire" <ptmcg@austin.r r._bogus_.com> wrote in message
        news:FkeZf.2791 $_26.1642@torna do.texas.rr.com ...[color=blue]
        > <tkpmep@hotmail .com> wrote in message
        > news:1144352710 .190839.165580@ z34g2000cwc.goo glegroups.com.. .[color=green]
        > > I have a list y[color=darkred]
        > > >>>y[/color]
        > > ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
        > > '19.500', '224']
        > >
        > > from which I want to extract only the 2nd and 4th item by partially
        > > unpacking the list. So I tried[color=darkred]
        > > >>>a,b = y[2,4][/color]
        > > Traceback (most recent call last):
        > > File "<interacti ve input>", line 1, in ?
        > > TypeError: list indices must be integers
        > >
        > > Out of curiosity, I tried[color=darkred]
        > > >>>a,b = y[2:4]
        > > >>>a[/color]
        > > '04276410'[color=darkred]
        > > >>> b[/color]
        > > '18.500'
        > >
        > > Why does this work (to a point - it gives me items 2 and 3, not 2 and
        > > 4 as I require) and not my first attempt? What is the right syntax to
        > > use when partially upacking a sequence?
        > >
        > > Thanks in advance
        > >
        > > Thomas Philips
        > >[/color]
        >
        > a,b = y[2],y[4]
        >
        > or
        >
        > a,b = y[2:5:2]
        >
        > or
        >
        > a,b = ( y[i] for i in (2,4) )
        >
        >
        > -- Paul
        >
        >[/color]

        Forgot one:

        _,_,a,_,b,_,_,_ = y

        There actually is some merit to this form. If the structure of y changes
        sometime in the future (specifically if a field is added or removed), this
        statement will fail noisily, calling your attention to this change. But if
        a new field is added, say at the front of the list, the previous methods
        will all silently succeed, but now giving you the values formerly known as
        y[1] and y[3].

        -- Paul


        Comment

        • tkpmep@hotmail.com

          #5
          Re: Partially unpacking a sequence

          Thank you, everyone, for resolving my question. At one point, while
          trying to solve the problem, I typed
          [color=blue][color=green][color=darkred]
          >>> y[1,3][/color][/color][/color]
          Traceback (most recent call last):
          File "<interacti ve input>", line 1, in ?
          TypeError: list indices must be integers

          The error message gave me no clue as to what I was doing wrong (in my
          mind, I was just writing out the elements of a range), and I thought
          perhaps that my inclusion of a comma was the problem. Perhaps a more
          explicit error message would have helped.

          Another solution, I suppose, is to use a list comprehension. Store the
          indices in a list x. For example, let x = [1,5,4]. Then

          a,b,c = [y[i] for i in x]

          Thanks

          Thomas Philips

          Comment

          • Fredrik Lundh

            #6
            Re: Partially unpacking a sequence

            Paul McGuire wrote:
            [color=blue]
            > Forgot one:
            >
            > _,_,a,_,b,_,_,_ = y
            >
            > There actually is some merit to this form. If the structure of y changes
            > sometime in the future (specifically if a field is added or removed), this
            > statement will fail noisily, calling your attention to this change. But if
            > a new field is added, say at the front of the list, the previous methods
            > will all silently succeed, but now giving you the values formerly known as
            > y[1] and y[3].[/color]

            if this is likely to happen, an straightforward assert is a lot easier to parse than
            a one-two-a-three-oops-one-two-a-four-five-etc number of underscores:

            assert len(y) == 8
            a = y[2]
            b = y[4]

            </F>



            Comment

            • Sybren Stuvel

              #7
              Re: Partially unpacking a sequence

              tkpmep@hotmail. com enlightened us with:[color=blue][color=green][color=darkred]
              >>>> y[1,3][/color][/color]
              > Traceback (most recent call last):
              > File "<interacti ve input>", line 1, in ?
              > TypeError: list indices must be integers
              >
              > The error message gave me no clue as to what I was doing wrong (in
              > my mind, I was just writing out the elements of a range), and I
              > thought perhaps that my inclusion of a comma was the problem.[/color]

              You thought correct. List indices must be integers, not tuples.
              [color=blue]
              > Perhaps a more explicit error message would have helped.[/color]

              Why? You thought the right thing.

              Sybren
              --
              The problem with the world is stupidity. Not saying there should be a
              capital punishment for stupidity, but why don't we just take the
              safety labels off of everything and let the problem solve itself?
              Frank Zappa

              Comment

              • jUrner@arcor.de

                #8
                Re: Partially unpacking a sequence

                >> Thank you, everyone, for resolving my question. At one point, while[color=blue][color=green]
                >> trying to solve the problem, I typed
                >>[color=darkred]
                >>>>> y[1,3][/color]
                >>
                >> Traceback (most recent call last):
                >> File "<interacti ve input>", line 1, in ?
                >> TypeError: list indices must be integers
                >>
                >> The error message gave me no clue as to what I was doing wrong (in my
                >>mind, I was just writing out the elements of a range), and I thought
                >> perhaps that my inclusion of a comma was the problem. Perhaps a more
                >>explicit error message would have helped.[/color][/color]


                The error message is correct because in y[1, 3] "1, 3" is recognized by
                the interpreter
                as tuple. Python goodie or snakebite that is...

                Comment

                • Fredrik Lundh

                  #9
                  Re: Partially unpacking a sequence

                  tkpmep@hotmail. com wrote:
                  [color=blue]
                  > Thank you, everyone, for resolving my question. At one point, while
                  > trying to solve the problem, I typed
                  >[color=green][color=darkred]
                  > >>> y[1,3][/color][/color]
                  > Traceback (most recent call last):
                  > File "<interacti ve input>", line 1, in ?
                  > TypeError: list indices must be integers
                  >
                  > The error message gave me no clue as to what I was doing wrong (in my
                  > mind, I was just writing out the elements of a range), and I thought
                  > perhaps that my inclusion of a comma was the problem. Perhaps a more
                  > explicit error message would have helped.[/color]

                  the problem is that the *compiler* doesn't know what "y" is, and y[1,3]
                  is a perfectly valid way to access e.g. a dictionary or a multidimensiona l
                  array. so it's the list implementation that has to do the complaining, and
                  all it knows is that it wants an integer index, and got something else.

                  maybe something like

                  TypeError: list indices must be integers (got tuple)

                  would have been less confusing; feel free to add a suggestion to the bug
                  tracker:

                  http://sourceforge.net/tracker/?grou...70&atid=105470

                  </F>



                  Comment

                  • bruno at modulix

                    #10
                    Re: Partially unpacking a sequence

                    tkpmep@hotmail. com wrote:[color=blue]
                    > I have a list y
                    >[color=green][color=darkred]
                    >>>>y[/color][/color]
                    >
                    > ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
                    > '19.500', '224']
                    >
                    > from which I want to extract only the 2nd and 4th item
                    >
                    > by partially
                    > unpacking the list. So I tried
                    >[color=green][color=darkred]
                    >>>>a,b = y[2,4][/color][/color][/color]

                    Mmm, so lovely and meaningful names !-)

                    FWIW, and since nobody seemed to mention it, list indexes are
                    zero-based, so the second element of a list is at index 1 and the fourth
                    at index 3.

                    Also, a GoodPractice(tm ) is to use named constants instead of magic
                    numbers. Here we don't have a clue about why these 2 elements are so
                    specials. Looking at the example list (which - semantically - should be
                    a tuple, not a list) I could wild-guess that the 2nd item is a reference
                    and the fourth a price, so:

                    REF_INDEX = 1 # lists are zero-based
                    PRICE_INDEX = 3

                    ref, price = y[REF_INDEX], y[PRICE_INDEX]

                    And finally, since this list is clearly structured data, wrapping it
                    into an object to hide away implementation details *may* help -
                    depending on the context, of course !-)


                    --
                    bruno desthuilliers
                    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                    p in 'onurb@xiludom. gro'.split('@')])"

                    Comment

                    Working...