473,383 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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
Jun 23 '06 #1
11 3695
dr*******@comcast.net wrote:
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?


No, since x[-2:0:-1] already has meaning and it isn't what you want.
--
Erik Max Francis && ma*@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
Jun 23 '06 #2
drtimh...@comcast.net wrote:
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


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

Jun 23 '06 #3
dr*******@comcast.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?

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

cheers,
fw

Jun 23 '06 #4
On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
dr*******@comcast.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.

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,
http://www.savrola.com/resources/negative_zero.html

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

Jun 23 '06 #5
Steven D'Aprano wrote:
On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
dr*******@comcast.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.


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

[...]
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!


Yeah, this is really cool ;-)

--
Filip

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

Jun 24 '06 #6
On Sat, 24 Jun 2006 05:36:17 -0700, Filip Wasilewski wrote:
Steven D'Aprano wrote:
On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
> dr*******@comcast.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.


I believe you will agree that this is mostly a matter of notation of
some fact rather than really trying to sign zero.


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.

Jun 25 '06 #7
Steven D'Aprano wrote:
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.


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 && ma*@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
Jun 25 '06 #8
On Sat, 24 Jun 2006 21:15:17 -0700, Erik Max Francis wrote:
Steven D'Aprano wrote:
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.


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


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

Jun 25 '06 #9
Steven D'Aprano wrote:
Steven D'Aprano wrote:
In matrix maths, there are an infinite number of
different matrices where all the elements are zero -- they are all
distinct, different, zeroes.

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


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.


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

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

--
Erik Max Francis && ma*@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
Jun 25 '06 #10
Steven D'Aprano wrote:
On Sat, 24 Jun 2006 05:36:17 -0700, Filip Wasilewski wrote:
Steven D'Aprano wrote:
On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:

> dr*******@comcast.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.
I believe you will agree that this is mostly a matter of notation of
some fact rather than really trying to sign zero.


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.


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


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

Jun 25 '06 #11
On 2006-06-23, Filip Wasilewski <fi*************@gmail.com> wrote:
dr*******@comcast.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?

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


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
Jun 26 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: dan | last post by:
I was recently surprised, and quite shocked in fact, to find that Python treats negative indices into sequence types as if they were mod(length-of-sequence), at least up to -len(seq). This fact...
19
by: David Abrahams | last post by:
Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this result very surprising, and would just like to see the...
9
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
8
by: Joakim Hove | last post by:
Hello, I have the following code: #define N 99 double *ptr; double *storage; int index; storage = calloc(N , sizeof(double));
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
2
by: smichr | last post by:
It seems to me that the indices() method for slices is could be improved. Right now it gives back concrete indices for a range of length n. That is, it does not return any None values. Using an...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.