What's the difference between these 2 statements?

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

    What's the difference between these 2 statements?

    What's the difference between these 2 statements?

    If you have a String s="12345"

    s[len(s)::-1] = "54321"

    But

    s[len(s):0:-1] = "5432"

    Why? What's the difference? What number then can I use as the end of
    the slice if I were to supply all 3 parameters?


    Thanks,
    AT
  • Reinhold Birkenfeld

    #2
    Re: What's the difference between these 2 statements?

    ATSkyWalker wrote:[color=blue]
    > What's the difference between these 2 statements?
    >
    > If you have a String s="12345"
    >
    > s[len(s)::-1] = "54321"
    >
    > But
    >
    > s[len(s):0:-1] = "5432"
    >
    > Why? What's the difference? What number then can I use as the end of
    > the slice if I were to supply all 3 parameters?[/color]

    -1.

    Reinhold

    Comment

    • tiissa

      #3
      Re: What's the difference between these 2 statements?

      Reinhold Birkenfeld wrote:[color=blue]
      > ATSkyWalker wrote:
      >[color=green]
      >>What's the difference between these 2 statements?
      >>
      >>If you have a String s="12345"
      >>
      >>s[len(s)::-1] = "54321"
      >>
      >>But
      >>
      >>s[len(s):0:-1] = "5432"
      >>
      >>Why? What's the difference? What number then can I use as the end of
      >>the slice if I were to supply all 3 parameters?[/color]
      >
      >
      > -1.[/color]

      -len(s) or less.
      -1 will return an empty string.

      Actually you start from len(s)-1 (len(s) is not an index in s) and you
      stop when you reach the index specified (or the end). Since -1 is the
      same index as the starting one (-1~>len(s)-1, -2~>len(s)-2,
      -len(s)+1~>0), you end up with an empty string.

      Therefore you have to try to reach indices lower (due to the negative
      step) than the minimum valid index of your list in order to reverse it
      fully.

      Comment

      • ahmedt@gmail.com

        #4
        Re: What's the difference between these 2 statements?

        s[len(s):-1:-1] yields an empty list !

        Test code :

        s = "12345"
        print s[len(s)::-1] -> prints "54321"
        print s[len(s):-1:-1] -> prints "" (nothing)

        Comment

        • Reinhold Birkenfeld

          #5
          Re: What's the difference between these 2 statements?

          tiissa wrote:[color=blue]
          > Reinhold Birkenfeld wrote:[color=green]
          >> ATSkyWalker wrote:
          >>[color=darkred]
          >>>What's the difference between these 2 statements?
          >>>
          >>>If you have a String s="12345"
          >>>
          >>>s[len(s)::-1] = "54321"
          >>>
          >>>But
          >>>
          >>>s[len(s):0:-1] = "5432"
          >>>
          >>>Why? What's the difference? What number then can I use as the end of
          >>>the slice if I were to supply all 3 parameters?[/color]
          >>
          >>
          >> -1.[/color]
          >
          > -len(s) or less.
          > -1 will return an empty string.
          >
          > Actually you start from len(s)-1 (len(s) is not an index in s) and you
          > stop when you reach the index specified (or the end). Since -1 is the
          > same index as the starting one (-1~>len(s)-1, -2~>len(s)-2,
          > -len(s)+1~>0), you end up with an empty string.
          >
          > Therefore you have to try to reach indices lower (due to the negative
          > step) than the minimum valid index of your list in order to reverse it
          > fully.[/color]

          Right, sorry.

          Well, I guess that's why one can leave out the index...

          Reinhold

          Comment

          • ahmedt@gmail.com

            #6
            Re: What's the difference between these 2 statements?

            I'm sorry, I'm not really following your logic. Can you supply the
            statement with the three parameters ?

            so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
            or is it impossible to express it in this way ?

            Thanks,
            AT

            Comment

            • Peter Otten

              #7
              Re: What's the difference between these 2 statements?

              ahmedt@gmail.co m wrote:
              [color=blue]
              > so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
              > or is it impossible to express it in this way ?[/color]

              This does not work for integers, because the theoretically correct value
              x = -1 already has another interpretation as the gap between the last and
              the last but one character. Here are two workarounds:

              1. Set x to None
              [color=blue][color=green][color=darkred]
              >>> s = "12345"
              >>> s[len(s):None:-1][/color][/color][/color]
              '54321'

              2. Separate slicing operation and reversal:
              [color=blue][color=green][color=darkred]
              >>> s = "12345"
              >>> s[0:len(s)][::-1][/color][/color][/color]
              '54321'

              Peter

              Comment

              • ATSkyWalker

                #8
                Re: What's the difference between these 2 statements?

                Peter,

                I like the way you put it "the gap between the last and
                the last but one character" :-).

                I guess this is a side effect of of python's asymetric slice indexing
                approach which takes a little getting used to.

                AT

                Comment

                • tiissa

                  #9
                  Re: What's the difference between these 2 statements?

                  ahmedt@gmail.co m wrote:[color=blue]
                  > I'm sorry, I'm not really following your logic. Can you supply the
                  > statement with the three parameters ?
                  >
                  > so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
                  > or is it impossible to express it in this way ?[/color]

                  Contrary to what I said above x should be _strictly_ less than -len(s).
                  You stop when you reach in the list the given end index (and don't take
                  the item there) or if you leave the index range.

                  But -1 as an index is the same as (len(s)-1).
                  Therefore going from len(s)-1 down to -1 is the same as going from
                  len(s)-1 to len(s)-1 hence an empty list.

                  And -len(s) is the same as 0 (my mistake above)
                  But -len(s)-1 is not in the list thus you won't discard any limit.

                  The example:
                  In [1]: s='12345'

                  In [2]: s[len(s)-1:0:-1]
                  Out[2]: '5432'

                  In [3]: s[len(s)-1:-1:-1]
                  Out[3]: ''

                  In [4]: s[-1],s[len(s)-1]
                  Out[4]: ('5', '5')

                  In [5]: s[len(s)-1:-len(s)-1:-1]
                  Out[5]: '54321'

                  In [6]: s[len(s)-1:-len(s):-1]
                  Out[6]: '5432'

                  Comment

                  • tiissa

                    #10
                    Re: What's the difference between these 2 statements?

                    Peter Otten wrote:[color=blue]
                    > ahmedt@gmail.co m wrote:
                    >
                    >[color=green]
                    >>so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
                    >>or is it impossible to express it in this way ?[/color]
                    >
                    >
                    > This does not work for integers, because the theoretically correct value
                    > x = -1 already has another interpretation as the gap between the last and
                    > the last but one character.[/color]
                    AFAIK, it is not an issue of integer (what else can an slice index be in
                    python?) but simply of index aliasing.

                    For x=-len(s)-1, you get the whole reversed list:

                    In [5]: s[len(s)-1:-len(s)-1:-1]
                    Out[5]: '54321'

                    Comment

                    • Peter Otten

                      #11
                      Re: What's the difference between these 2 statements?

                      tiissa wrote:
                      [color=blue]
                      > Peter Otten wrote:[color=green]
                      >> ahmedt@gmail.co m wrote:
                      >>
                      >>[color=darkred]
                      >>>so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
                      >>>or is it impossible to express it in this way ?[/color]
                      >>
                      >>
                      >> This does not work for integers, because the theoretically correct value
                      >> x = -1 already has another interpretation as the gap between the last and
                      >> the last but one character.[/color]
                      > AFAIK, it is not an issue of integer (what else can an slice index be in
                      > python?) but simply of index aliasing.
                      >
                      > For x=-len(s)-1, you get the whole reversed list:
                      >
                      > In [5]: s[len(s)-1:-len(s)-1:-1]
                      > Out[5]: '54321'[/color]

                      Clever. I didn't think of that.
                      Still, for practical purposes you have to test for slicelen >= stringlen, so
                      whether you choose None, -len(s)-1, or -sys.maxint as the second slice
                      parameter doesn't matter much.

                      Peter

                      Comment

                      • tiissa

                        #12
                        Re: What's the difference between these 2 statements?

                        Peter Otten wrote:[color=blue]
                        > Still, for practical purposes you have to test for slicelen >= stringlen, so
                        > whether you choose None, -len(s)-1, or -sys.maxint as the second slice
                        > parameter doesn't matter much.[/color]

                        Sure, for practical purposes you don't bother to write extra characters
                        and leave it void.
                        But we knew it from the start of the thread. ;)

                        Comment

                        Working...