Many people I know ask why Python does slicing the way it does.....
Can anyone /please/ give me a good defense/justification?? ?
I'm referring to why mystring[:4] gives me
elements 0, 1, 2 and 3 but *NOT* mystring[4] (5th element).
Many people don't like idea that 5th element is not invited.
(BTW, yes I'm aware of the explanation where slicing
is shown to involve slices _between_ elements. This
doesn't explain why this is *best* way to do it.)
Chris
Jul 19 '05
54 3982
Bernhard Herzog <bh@intevation. de> writes: There are very good reasons for half-open intervals and starting at 0 apart from memory organization. Dijkstra explained this quite well in http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
Thanks for the excellent link!
--
--------------------------------------------------------------------
Aaron Bingham
Software Engineer
Cenix BioScience GmbH
--------------------------------------------------------------------
Hallöchen!
Bernhard Herzog <bh@intevation. de> writes: Torsten Bronger <br*****@physik .rwth-aachen.de> writes:
It's interesting to muse about a language that starts at "1" for all arrays and strings, as some more or less obsolete languages do. I think this is more intuitive, since most people (including mathematicians) start counting at "1". The reason for starting at "0" is easier memory address calculation, so nothing for really high level languages.
There are very good reasons for half-open intervals and starting at 0 apart from memory organization. Dijkstra explained this quite well in http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
I see only one argument there: "Inclusion of the upper bound would
then force the latter to be unnatural by the time the sequence has
shrunk to the empty one." While this surely is unaesthetical, I
don't think that one should optimise syntax according to this very
special case. Besides, no language I know of has probems with
negative values.
Well, and the argument for "0" derives from that, according to
Dijkstra.
Tschö,
Torsten.
--
Torsten Bronger, aquisgrana, europa vetus
Terry Hancock wrote: However, I used to make "off by one" errors all the time in both C and Fortran, whereas I hardly ever make them in Python.
This should probably be the overriding concern in this
case.
I can't remember the last time I made an off-by-one error
in Python (or, really, whether I ever have), whereas I
can't remember the last C program I wrote which didn't have
one.
So I like Python's slicing because it "bites *less*" than intervals in C or Fortran.
+1 QOTW
Torsten Bronger <br*****@physik .rwth-aachen.de> writes: http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
I see only one argument there: "Inclusion of the upper bound would then force the latter to be unnatural by the time the sequence has shrunk to the empty one." While this surely is unaesthetical, I don't think that one should optimise syntax according to this very special case.
The other main argument for startig at 0 is that if you do not include
the upper bound and start at 1 then the indices i of a sequence of N
values are 1 <= i < N + 1 which is not as nice as 0 <= i < N.
opportunity for an off by one error.
Then there's also that, starting at 0, "an element's ordinal (subscript)
equals the number of elements preceding it in the sequence."
Bernhard
--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
Hallöchen!
Bernhard Herzog <bh@intevation. de> writes: Torsten Bronger <br*****@physik .rwth-aachen.de> writes:
http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF I see only one argument there: "Inclusion of the upper bound would then force the latter to be unnatural by the time the sequence has shrunk to the empty one." [...]
The other main argument for startig at 0 is that if you do not include the upper bound and start at 1 then the indices i of a sequence of N values are 1 <= i < N + 1 which is not as nice as 0 <= i < N. opportunity for an off by one error.
The alternative is starting with 1 and using "lower <= i <= upper".
(Dijkstra's second choice.)
Then there's also that, starting at 0, "an element's ordinal (subscript) equals the number of elements preceding it in the sequence."
Granted, but you trade such elegancies for other uglinesses. A
couple of times I changed the lower limit of some data structure
from 0 to 1 or vice versa, and ended up exchanging a "+1" here for a
"-1" there.
It's a matter of what you are accustomed to, I suspect. We
(programmers) think with the 0-notation, but non-spoiled minds
probably not.
Tschö,
Torsten.
--
Torsten Bronger, aquisgrana, europa vetus
Terry Hancock wrote: I used to make "off by one" errors all the time in both C and Fortran, whereas I hardly ever make them in Python.
Part of the reason may be that most loops over lists involve
iterators, where the details of the index limits are hidden. In
Python, you write:
for item in myList:
blah
but in C and Fortran you would write:
for (i = 0; i < MAXLIST; ++i) {
blah;
do 10 i = 1, MAXLIST
10 blah
both endpoints are mentioned explicitly. C++/STL also uses iterators,
but the syntax is repulsive.
On Tuesday 19 April 2005 10:58 pm, se******@spawar .navy.mil wrote: Many people I know ask why Python does slicing the way it does.....
Can anyone /please/ give me a good defense/justification?? ?
Here you go, no remembering "+1" or "-1". Also, see the hundreds of other
times this topic has graced this list. i = 4 str = "asdfjkl;" print str[:i]+str[i:]
asdfjkl;
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/
If you have five elements, numbered 0,1,2,3,4 and you ask for the
elements starting with the first one, and so on for the length you
would have [0:length]. So [0:5] gives you elemets 0,1,2,3,4. Think
of the weirdess if you had to ask for [0:length-1] to get length
elements...
One based 1... n are what I call _counting numbers_
Zero based 0... n-1 are the _indexes_ (offsets) into the collection.
The first element is at offset 0.
It is a little weired that slicing does [index: count] instead of
[index:index] or [count:count] I agree, but python really does just
flow wonderfully once you see how clean code is that's written [index:
count].
In C++ the STL also has the idea that there's an 'end()' iterator that
is really one element past the end of your container. It makes things
flow really well there too. All code interates up to but not
including the last element you specify. always.
-Jim
On 19 Apr 2005 22:58:49 -0700, se******@spawar .navy.mil
<se******@spawa r.navy.mil> wrote: Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification?? ? I'm referring to why mystring[:4] gives me elements 0, 1, 2 and 3 but *NOT* mystring[4] (5th element). Many people don't like idea that 5th element is not invited. (BTW, yes I'm aware of the explanation where slicing is shown to involve slices _between_ elements. This doesn't explain why this is *best* way to do it.) Chris -- http://mail.python.org/mailman/listinfo/python-list
I like this, it works for any integer. str="asdfjkl;" i=-400 print str[:i]+str[i:]
asdfjkl; i = 65534214 print str[:i]+str[i:]
asdfjkl;
Please forgive my reassigning str. I am one of those type first think later
programmers.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/
<se******@spawa r.navy.mil> escribió en el mensaje
news:11******** **************@ o13g2000cwo.goo glegroups.com.. . Many people I know ask why Python does slicing the way it does.....
Can anyone /please/ give me a good defense/justification?? ?
I'm referring to why mystring[:4] gives me elements 0, 1, 2 and 3 but *NOT* mystring[4] (5th element).
Many people don't like idea that 5th element is not invited.
(BTW, yes I'm aware of the explanation where slicing is shown to involve slices _between_ elements. This doesn't explain why this is *best* way to do it.)
Recently there was a short (sub)thread about that.
One of my messages (against half-open slices) is,
for example http://groups-beta.google.com/group/...32dd50b57853b1
Javier
_______________ _______________ _______________ ______________
Javier Bezos | TeX y tipografía
jbezos at wanadoo dot es | http://perso.wanadoo.es/jbezos
............... ..............| ............... ............... .
CervanTeX (Spanish TUG) | http://www.cervantex.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christian Neumann |
last post by:
Hello,
i have a problem with the built-in function xrange(). Could you by any
chance be able to help?
I use Python 2.3.4 (final) and i think there is a bug in the built-in
|
by: Just |
last post by:
While googling for a non-linear equation solver, I found
Math::Polynomial::Solve in CPAN. It seems a great little module, except
it's not Python... I'm especially looking for its poly_root()
functionality (which solves arbitrary polynomials). Does anyone know of
a Python module/package that implements that?
Just
|
by: jbperez808 |
last post by:
>>> rs='AUGCUAGACGUGGAGUAG'
>>> rs='GAG'
Traceback (most recent call last):
File "<pyshell#119>", line 1, in ?
rs='GAG'
TypeError: object doesn't support slice assignment
You can't assign to a section of a sliced string in
Python 2.3 and there doesn't seem to be mention of this
as a Python 2.4 feature (don't have time to actually try
|
by: Michael Tobis |
last post by:
Someone asked me to write a brief essay regarding the value-add
proposition for Python in the Fortran community. Slightly modified to
remove a few climatology-related specifics, here it is.
I would welcome comments and corrections, and would be happy to
contribute some version of this to the Python website if it is of
interest.
===
|
by: Joel Hedlund |
last post by:
Hi!
The question of type checking/enforcing has bothered me for a while, and
since this newsgroup has a wealth of competence subscribed to it, I
figured this would be a great way of learning from the experts. I feel
there's a tradeoff between clear, easily readdable and extensible code
on one side, and safe code providing early errors and useful tracebacks
on the other. I want both! How do you guys do it? What's the pythonic
way? Are...
| |
by: kath |
last post by:
How do I read an Excel file in Python?
I have found a package to read excel file, which can be used on any
platform.
http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell's
contents in a different manner.
print sh.row(rx)
|
by: Kurt B. Kaiser |
last post by:
Patch / Bug Summary
___________________
Patches : 420 open ( +6) / 3510 closed (+12) / 3930 total (+18)
Bugs : 944 open ( -5) / 6391 closed (+15) / 7335 total (+10)
RFE : 249 open ( +2) / 245 closed ( +0) / 494 total ( +2)
New / Reopened Patches
______________________
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |