String negative indices?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • drtimhill@comcast.net

    #1

    String negative indices?

    I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.

    For example, to get the last character in a string, I can enter "x[-1]". To get the 2nd and 3rd to last, I can enter x[-3:-1] etc. This is fine.

    Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].

    With simple constants, this is ok, but it's a little more annoying when the start and end of the range are in variables somewhere. The only way I can find of making this work without lots of special-case "if" logic is to translate negative subscripts to positive, which kinda defeats the purpose of the negative subscripts anyway.

    Is there some magic I'm missing here? Wouldn't it actually be better for Python to treat 0 as a special case here, so that x[-2:0] and x[-2:] generated the same result?

    --Tim
  • Erik Max Francis

    #2
    Re: String negative indices?

    drtimhill@comca st.net wrote:
    [color=blue]
    > Is there some magic I'm missing here? Wouldn't it actually be better for Python to treat 0 as a
    > special case here, so that x[-2:0] and x[-2:] generated the same result?[/color]

    No, since x[-2:0:-1] already has meaning and it isn't what you want.


    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    The mind is not a vessel to be filled but a fire to be kindled.
    -- Plutarch

    Comment

    • George Sakkis

      #3
      Re: String negative indices?

      drtimh...@comca st.net wrote:
      [color=blue]
      > I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.
      >
      > For example, to get the last character in a string, I can enter "x[-1]". To get the 2nd and 3rd to last, I can enter x[-3:-1] etc. This is fine.
      >
      > Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].
      >
      > With simple constants, this is ok, but it's a little more annoying when the start and end of the range are in variables somewhere. The only way I can find of making this work without lots of special-case "if" logic is to translate negative subscripts to positive, which kinda defeats the purpose of the negative subscripts anyway.
      >
      > Is there some magic I'm missing here? Wouldn't it actually be better for Python to treat 0 as a special case here, so that x[-2:0] and x[-2:] generated the same result?
      >
      > --Tim[/color]

      x[-2:None]

      I'm not sure if it qualifies as "magic" but you're right, it's not more
      obvious than 0 would be.

      George

      Comment

      • Filip Wasilewski

        #4
        Re: String negative indices?

        drtimhill@comca st.net wrote:
        [color=blue]
        > Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].[/color]

        Hooray! Logically there is no such thing as positive or negative zero,
        or did I miss something in the primary?

        PS. x[len(x)-2 : len(x)-0]

        cheers,
        fw

        Comment

        • Steven D'Aprano

          #5
          Re: String negative indices?

          On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
          [color=blue]
          > drtimhill@comca st.net wrote:
          >[color=green]
          >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].[/color]
          >
          > Hooray! Logically there is no such thing as positive or negative zero,
          > or did I miss something in the primary?[/color]

          No, not in the primary, or even in the secondary, but possibly in the
          tertiary.

          For many purposes, it doesn't make sense to distinguish +0 from -0. But
          for other purposes, it does.

          For instance, in floating point maths, it may be useful for negative
          numbers that underflow to be distinguished from positive numbers that
          underflow. See, for example,


          In statistical mechanics, some systems can have negative absolute
          temperatures, including negative zero. Counter-intuitively, negative
          absolute temperatures aren't colder than absolute zero, but hotter than
          any positive temperature. So, strangely enough, a temperature of -0K is
          hotter than a infinitely hot temperature!

          (Those wacky physicists and their mathematics...)

          See http://en.wikipedia.org/wiki/Negativ...te_temperature for a
          description. And in case you think this is just a modern version of angels
          dancing on the head of a pin, negative absolute temperatures are essential
          for lasers.

          In pure mathematics, zero is usually considered unsigned. However, in
          "non-standard analysis" using so-called "hyperreal" or "surreal" numbers,
          mathematicians use infinitesimals which are [sloppy hand-waving] like
          signed zeroes. To put it another way, only slightly less sloppy,
          infinitesimals are zero, but not all the same zero.

          When doing calculus with complex numbers, it is very important to
          distinguish which direction you are taking your limits in, and so
          lim z -> 0+0i is not necessarily the same as lim z -> 0-0i.


          --
          Steven

          Comment

          • Filip Wasilewski

            #6
            Re: String negative indices?

            Steven D'Aprano wrote:[color=blue]
            > On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
            >[color=green]
            > > drtimhill@comca st.net wrote:
            > >[color=darkred]
            > >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].[/color]
            > >
            > > Hooray! Logically there is no such thing as positive or negative zero,
            > > or did I miss something in the primary?[/color]
            >
            > No, not in the primary, or even in the secondary, but possibly in the
            > tertiary.
            >
            > For many purposes, it doesn't make sense to distinguish +0 from -0. But
            > for other purposes, it does.[/color]

            I believe you will agree that this is mostly a matter of notation of
            some fact rather than really trying to sign zero.
            For example I use to denote numbers that approach zero from "the right
            side" by x -> [0 uppercase +], which is definitely harder to type and
            display in poor man's text editor than x -> +0.
            Otherwise I should also rethink the meaning of positive (>0) and
            negative (<0) numbers (at least where such relations are defined).

            [...]
            [color=blue]
            > In statistical mechanics, some systems can have negative absolute
            > temperatures, including negative zero. Counter-intuitively, negative
            > absolute temperatures aren't colder than absolute zero, but hotter than
            > any positive temperature. So, strangely enough, a temperature of -0K is
            > hotter than a infinitely hot temperature![/color]

            Yeah, this is really cool ;-)

            --
            Filip

            "During board meeting: We have great news. Our earnings reached highest
            value ever - a positive 0."

            Comment

            • Steven D'Aprano

              #7
              Re: String negative indices?

              On Sat, 24 Jun 2006 05:36:17 -0700, Filip Wasilewski wrote:
              [color=blue]
              > Steven D'Aprano wrote:[color=green]
              >> On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
              >>[color=darkred]
              >> > drtimhill@comca st.net wrote:
              >> >
              >> >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].
              >> >
              >> > Hooray! Logically there is no such thing as positive or negative zero,
              >> > or did I miss something in the primary?[/color]
              >>
              >> No, not in the primary, or even in the secondary, but possibly in the
              >> tertiary.
              >>
              >> For many purposes, it doesn't make sense to distinguish +0 from -0. But
              >> for other purposes, it does.[/color]
              >
              > I believe you will agree that this is mostly a matter of notation of
              > some fact rather than really trying to sign zero.[/color]

              No.

              In floating point, +0 *really is* different from -0 -- the two floats
              have different bit patterns. Floating point libraries must be specifically
              programmed to ignore that difference, making zero treated differently than
              other floats. That's -- usually -- a good thing.

              In mathematics, well, maybe... certainly in the Real number system, there
              is no difference, and +0 and -0 are just two ways of writing the same
              thing. In the hyperreals, +0 and -0 are the same, but there are
              infinitesimals which are different, and signed. I don't know enough about
              the surreals to comment. In matrix maths, there are an infinite number of
              different matrices where all the elements are zero -- they are all
              distinct, different, zeroes.

              And in "the real world", all numbers are an abstraction anyway, so I'm
              not too worried about correspondence to reality. If mathematicians find a
              use for distinguishing +0 from -0 (as the statistical physicists have
              done) they will treat them as "really" different. If they don't, they
              won't.


              --
              Steven.

              Comment

              • Erik Max Francis

                #8
                Re: String negative indices?

                Steven D'Aprano wrote:
                [color=blue]
                > In mathematics, well, maybe... certainly in the Real number system, there
                > is no difference, and +0 and -0 are just two ways of writing the same
                > thing. In the hyperreals, +0 and -0 are the same, but there are
                > infinitesimals which are different, and signed. I don't know enough about
                > the surreals to comment. In matrix maths, there are an infinite number of
                > different matrices where all the elements are zero -- they are all
                > distinct, different, zeroes.[/color]

                What do you even mean by that? By "matrix maths," do you just mean
                matrices whose elements are reals, or something else?

                --
                Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                My heart is pure as the driven slush.
                -- Tallulah Bankhead

                Comment

                • Steven D'Aprano

                  #9
                  Re: String negative indices?

                  On Sat, 24 Jun 2006 21:15:17 -0700, Erik Max Francis wrote:
                  [color=blue]
                  > Steven D'Aprano wrote:
                  >[color=green]
                  >> In mathematics, well, maybe... certainly in the Real number system, there
                  >> is no difference, and +0 and -0 are just two ways of writing the same
                  >> thing. In the hyperreals, +0 and -0 are the same, but there are
                  >> infinitesimals which are different, and signed. I don't know enough about
                  >> the surreals to comment. In matrix maths, there are an infinite number of
                  >> different matrices where all the elements are zero -- they are all
                  >> distinct, different, zeroes.[/color]
                  >
                  > What do you even mean by that? By "matrix maths," do you just mean
                  > matrices whose elements are reals, or something else?[/color]

                  Given any matrix M, there is a matrix Z such that M+Z = M. That matrix Z
                  is equivalent to zero in the reals, where x+0 = x.

                  In the reals, there is only one "zero", 0.

                  In matrices, there are an infinite number of "zeroes":

                  1x1 matrix: [0]
                  1x2 matrix: [0 0]
                  1x3 matrix: [0 0 0]
                  2x2 matrix:
                  [0 0]
                  [0 0]

                  etc. It is true that none of these are exactly equivalent to +0 and -0,
                  but my point was that there can be more than one distinct zero in pure
                  mathematics, and there is nothing wrong with the concept of a system with
                  more than one distinct zero.


                  --
                  Steven

                  Comment

                  • Erik Max Francis

                    #10
                    Re: String negative indices?

                    Steven D'Aprano wrote:
                    [color=blue][color=green]
                    >> Steven D'Aprano wrote:
                    >>[color=darkred]
                    >>> In matrix maths, there are an infinite number of
                    >>> different matrices where all the elements are zero -- they are all
                    >>> distinct, different, zeroes.[/color][/color]
                    >[color=green]
                    >> What do you even mean by that? By "matrix maths," do you just mean
                    >> matrices whose elements are reals, or something else?[/color]
                    >
                    > Given any matrix M, there is a matrix Z such that M+Z = M. That matrix Z
                    > is equivalent to zero in the reals, where x+0 = x.[/color]

                    Ah, of course. I knew this, I just misinterpreted your "distinct,
                    different, zeroes" as referring to the elements but not the matrices.
                    Just a misunderstandin g.

                    Every zero matrix is an additive identity, and there are an infinite
                    number of them. That's certainly true.

                    --
                    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                    There's a reason why we / Keep chasing morning
                    -- Sandra St. Victor

                    Comment

                    • Filip Wasilewski

                      #11
                      Re: String negative indices?

                      Steven D'Aprano wrote:[color=blue]
                      > On Sat, 24 Jun 2006 05:36:17 -0700, Filip Wasilewski wrote:
                      >[color=green]
                      > > Steven D'Aprano wrote:[color=darkred]
                      > >> On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
                      > >>
                      > >> > drtimhill@comca st.net wrote:
                      > >> >
                      > >> >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].
                      > >> >
                      > >> > Hooray! Logically there is no such thing as positive or negative zero,
                      > >> > or did I miss something in the primary?
                      > >>
                      > >> No, not in the primary, or even in the secondary, but possibly in the
                      > >> tertiary.
                      > >>
                      > >> For many purposes, it doesn't make sense to distinguish +0 from -0. But
                      > >> for other purposes, it does.[/color]
                      > >
                      > > I believe you will agree that this is mostly a matter of notation of
                      > > some fact rather than really trying to sign zero.[/color]
                      >
                      > No.
                      >
                      > In floating point, +0 *really is* different from -0 -- the two floats
                      > have different bit patterns. Floating point libraries must be specifically
                      > programmed to ignore that difference, making zero treated differently than
                      > other floats. That's -- usually -- a good thing.[/color]

                      So does the fact that the "-0.0" and "+0.0" strings can be converted by
                      C compilator to floats and stored with different bit patterns means
                      that they can behave much different in computation and comparison than
                      real numbers written in an expression on a piece of paper? Rather not.
                      Floating point is only a (very useful) finite precision model (there
                      are others) of real (rational) numbers and these differences are
                      implementation details for me.

                      And as far as the division by signed floating point zero and similar
                      operations in IEEE 754 are concerned, again, this is only a model in
                      which 0.0 means a value that is either equal to real zero or greater,
                      but too small to be distinguished from zero using finite amount of bits
                      (similarly -0.0), so the expressions like
                      1.0/-0.0 -> -1.#INF
                      1.0/0.0 -> 1.#INF
                      have some justification, although being meaningless in ordinary
                      arithmetic.

                      To be not so off-topic, I also expect from Python's integer, float and
                      Decimal computer formats to behave as much similar to their equivalents
                      known from math lessons as possible, which in particular means '=='
                      equality of -0 and 0, -0.0 and 0.0, and Decimal("-0") and Decimal("0"),
                      no matter what.
                      [color=blue]
                      > In mathematics, well, maybe... certainly in the Real number system, there
                      > is no difference, and +0 and -0 are just two ways of writing the same
                      > thing. In the hyperreals, +0 and -0 are the same, but there are
                      > infinitesimals which are different, and signed. I don't know enough about
                      > the surreals to comment. In matrix maths, there are an infinite number of
                      > different matrices where all the elements are zero -- they are all
                      > distinct, different, zeroes.
                      >
                      > And in "the real world", all numbers are an abstraction anyway, so I'm
                      > not too worried about correspondence to reality. If mathematicians find a
                      > use for distinguishing +0 from -0 (as the statistical physicists have
                      > done) they will treat them as "really" different. If they don't, they
                      > won't.[/color]

                      As far as math stuff is concerned I will make it short. You can't deny
                      that there is still unique additive identity in an additive group.

                      I also wouldn't mix symbols of physics states or others with ordinary
                      math expressions as this is only a matter of notation of some concept
                      (feel free to use any symbol you want).

                      Thanks for stimulating my neural cells, cheers,
                      fw

                      Comment

                      • Antoon Pardon

                        #12
                        Re: String negative indices?

                        On 2006-06-23, Filip Wasilewski <filipwasilewsk i@gmail.com> wrote:[color=blue]
                        > drtimhill@comca st.net wrote:
                        >[color=green]
                        >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].[/color]
                        >
                        > Hooray! Logically there is no such thing as positive or negative zero,
                        > or did I miss something in the primary?
                        >
                        > PS. x[len(x)-2 : len(x)-0][/color]

                        This seems to defeat the purpose of allowing negative indexes. My
                        understanding was that negative indexes were introduced to avoid
                        things like seq[len(seq) - i]

                        --
                        Antoon Pardon

                        Comment

                        Working...