Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I thought the first index, whether going forward or backward, was
inclusive. And there is no index of 10 in this list, so what is it
referring to?
Thanks. 11 1312
John Salerno wrote: Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this list, so what is it referring to?
Thanks.
Its referring to right after the end of the list, just as the 0 refers
to just before the start of the list. The word "slice" is supposed to
suggest cutting between (or before or after) elements.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/
John Salerno wrote: Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I always have trouble with these. Given the docs[1]:
"""
The slice of s from i to j with step k is defined as the sequence of
items with index x = i + n*k such that $0 \leq n < \frac{j-i}{k}$. In
other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping
when j is reached (but never including j). If i or j is greater than
len(s), use len(s). If i or j are omitted then they become ``end''
values (which end depends on the sign of k). Note, k cannot be zero.
"""
I would expect that we're looking at the indices
10 + n*(-2)
such that
0 <= n < (0-10)/-2
0 <= n < 5
which should be indices 10, 8, 6, 4, 2 which would skip 10 so as not to
provoke an IndexError, and then give you the list [9, 7, 3, 5, 1].
Clearly that's not what happens. Looks like there's a rule in there
somewhere that says if the start is greater than the length, use the
length instead. That would mean starting at 9 instead of 10, and
getting the indices 9, 7, 5, 3, 1 which would give you the list [10, 8,
6, 4, 2]
Hmm... Is there some documentation I'm missing that says the start
index will be replaced with the length if necessary? I'll file a doc
patch if it looks like I'm not just missing something somewhere else...
STeVe
[1]http://docs.python.org/lib/typesseq.html
John Salerno wrote: Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I think the documentation is misleading/incomplete when
it comes to negative strides for extended slices.
The relevent sections are here: http://www.python.org/doc/2.3.5/ref/types.html
[quote]
Some sequences also support "extended slicing" with a
third "step" parameter: a[i:j:k] selects all items of a
with index x where x = i + n*k, n >= 0 and i <= x < j.
[end quote]
and from http://www.python.org/doc/2.3.5/ref/slicings.html
[quote]
It is not an error if i or j lie outside the range of
valid indexes (such items don't exist so they aren't
selected).
[end quote]
The documentation suggests that, given a slice
[10:0:-2], Python looks up indices:
10 + 0*-2, 10 + 1*-2, 10 + 2*-2, ...
or 10, 8, 6, 4, 2, 0, ...
but since *none* of these indices is within the limits
10 <= x and x < 0, the documentation suggests that
the result should be the empty list.
The indices actually selected are:
9, 7, 5, 3, 1
at which point I give up and throw my hands in the air
and promise never to use negative strides with extended
slices.
--
Steven.
James Stroud wrote: John Salerno wrote: Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this list, so what is it referring to?
Thanks.
Its referring to right after the end of the list, just as the 0 refers to just before the start of the list. The word "slice" is supposed to suggest cutting between (or before or after) elements.
But why isn't the first index inclusive in this case?
John Salerno wrote: James Stroud wrote:
John Salerno wrote:
Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this list, so what is it referring to?
Thanks.
Its referring to right after the end of the list, just as the 0 refers to just before the start of the list. The word "slice" is supposed to suggest cutting between (or before or after) elements.
But why isn't the first index inclusive in this case?
You skipped it with the -2 step size.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/
Steven D'Aprano wrote: John Salerno wrote:
Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]? I think the documentation is misleading/incomplete when it comes to negative strides for extended slices.
Agreed!
[snip...] The indices actually selected are:
9, 7, 5, 3, 1
at which point I give up and throw my hands in the air and promise never to use negative strides with extended slices.
!!! Agreed!
I found this which seems an appropriate description:
"""
An extended slice of list x of length n in the form x[j:k:i] selects
every i-th element starting with and including the element at index j
and ending with but not including the element at index k. When either
index is negative, the value of n is added to it before any further
processing occurs. When either index is missing or lies outside of the
list bounds, the minimum or maximum inclusive index is used
automatically.
"""
source: https://trove.homeip.net/ExtendedSlices
André
-- Steven.
André wrote: An extended slice of list x of length n in the form x[j:k:i] selects every i-th element starting with and including the element at index j
This makes it sound like the index of 10 should be inclusive.
When either index is missing or lies outside of the list bounds, the minimum or maximum inclusive index is used automatically.
So does that mean that the example actually is wrong, and Python is
compensating? I tried numbers[9:0:-2] and it returned the same value as
if I had used 10 as the first index, so perhaps 10 shouldn't be there
after all.
> John Salerno wrote: Given: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] can someone explain to me why numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
It appears that s[i:j:-1] is s[(j+1):(i+1)] .reverse()'ed. For 'numbers',
this is 10, 9, 8, 7, 6, 5, 4, 3, 2]. Then take every other item. Why the
+1? Don't know and not my intuitive expectation. I just know that
extended slicing was developed for and until recently pretty much
restricted to numeric (now numpy).
Steven D'Aprano I think the documentation is misleading/incomplete when it comes to negative strides for extended slices.
and Andre "Agreed!" also, and you three aren't the only ones. Maybe some
day I will read the source, think about it more, and post a suggested
revision for comment ... or maybe someone will beat me to it.
Terry Jan Reedy
Terry Reedy wrote: John Salerno wrote: Given: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] can someone explain to me why numbers[10:0:-2] results in [10, 8, 6, 4, 2]? It appears that s[i:j:-1] is s[(j+1):(i+1)] .reverse()'ed. For 'numbers', this is 10, 9, 8, 7, 6, 5, 4, 3, 2]. Then take every other item. Why the +1? Don't know and not my intuitive expectation. I just know that extended slicing was developed for and until recently pretty much restricted to numeric (now numpy).
It's not simply "+1". a = range(10) a[9:0:-2]
[9, 7, 5, 3, 1]
a[10:0:-2]
[9, 7, 5, 3, 1]
a[11:0:-2]
[9, 7, 5, 3, 1]
a[42:0:-2]
[9, 7, 5, 3, 1]
Steven D'Aprano I think the documentation is misleading/incomplete when it comes to negative strides for extended slices. and Andre "Agreed!" also, and you three aren't the only ones. Maybe some day I will read the source, think about it more, and post a suggested revision for comment ... or maybe someone will beat me to it.
I, myself, think that the slicing behaviour is broken in this case.From the little I understand (without looking at the source),
adjustment on the boundaries of the slicing "request" (the first 2
numbers) is done first, to make them positive and, I believe, ensure
that they do not extend beyond the boundaries of the sequence. Then,
the actual stepping through is performed.
In my (most likely not-well informed enough opinion), the behaviour
should be the following:
from [i: j: k],
if k > 0 and j <= i : return []
if k< 0 and j >= i : return []
otherwise, build the following list of numbers:
i, i+k, i+2*k,
until i +n*k >= j (if k>0; use <= if k<0)
where the last number is excluded, i.e.
[0: 2: 1] = list(0, 1)
*Then*, exclude from the list just built any negative numbers, or any
number greater than the length of the sequence to be sliced.
So, [10:0:-2] would yield the following list of numbers
[10, 8, 6, 4, 2] before trimming; applying this to
range(10), would end up with result in trimming "10" from the list
built.
Thus, we are looking at indices [8, 6, 4, 2] of range(10), which happen
to be the numbers [8, 6, 4, 2] in this case. We should end up with
the same list if we ask for [42:0:-2]. This is not the observed
behaviour,
as we get [9, 7, 5, 3, 1].
If we ask Python to do a[0:10:2]
we get
[0, 2, 4, 6, 8]
which is correct. It seems to me that the following (pseudo-code, not
valid Python)
a[2:10:2] == a[8:0:-2].reversed()
should be true, just like
a[1:10:1] == a[9:0:-1].reversed()
is true.
I *suspect* that "boundary adjustments" are performed first before
creating a sequence of indices, so that there is less "trimming" (as
described above) for the sake of efficiency ... and that this is not
done properly.
Then again, perhaps I simply don't understand what slices are supposed
to do....
André Terry Jan Reedy
John Salerno wrote: Given:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
can someone explain to me why
numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I've filed a bug report: http://bugs.python.org/1446619
I suggest the following rewording for extended slices:
"""
To get the slice of s from i to j with step k, first
determine the bounds. If k is positive, and i or j is
greater than len(s), use len(s). If k is negative, and
i or j is greater than len(s)-1, use len(s)-1. Note, k
cannot be zero. If i or j are omitted then they become
``end'' values (which end depends on the sign of k).
The slice of s from i to j with step k is then defined
as the sequence of items with index x = i + n*k such
that 0 <= n < (j - i)/k. In other words, the indices
are i, i+k, i+2*k, i+3*k and so on, stopping when j is
reached (but never including j).
"""
I believe that properly explains the behavior, but if I've messed it up
(entirely likely), please suggest alternate wording on the bug report.
STeVe
"André" <an***********@gmail.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com... Terry Reedy wrote: It appears that s[i:j:-1] is s[(j+1):(i+1)] .reverse()'ed. For 'numbers', this is 10, 9, 8, 7, 6, 5, 4, 3, 2]. Then take every other item. Why the +1? Don't know and not my intuitive expectation. I just know that extended slicing was developed for and until recently pretty much
<> restricted to numeric (now numpy). It's not simply "+1".
Yes it is. Boundary adjustment follows, as implied by what I wrote. a = range(10) a[9:0:-2] a[10:0:-2] a[11:0:-2] a[42:0:-2]
are all [9, 7, 5, 3, 1] because a[1:10] == a[1:11] == a[1:12] == a[1:43] ==
[1,3,5,7,9], which is then reversed.
Terry Jan Reedy This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Henning Kage |
last post by:
I'm using Python only for some months now and I'm wondering, whether such
assignments as above are creating bitwise copies of an object or just
recieve a reference. That means I wanted to know,...
|
by: Jerry Sievers |
last post by:
Fellow Pythonists;
I am totally puzzled on the use of slicing on mapping types and
especially unsure on use of the Ellipsis... and slicing syntax that
has two or more groups seperated by comma....
|
by: seberino |
last post by:
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 gives me
elements 0, 1, 2 and 3...
|
by: Steven Bethard |
last post by:
In trying to work out what's different between the start, stop and step
of slice.indices() and the start, stop and step of sequence slicing I
found that some of the list slicing documentation is...
|
by: Bryan Olson |
last post by:
I recently wrote a module supporting value-shared slicing. I
don't know if this functionality already existed somewhere, but
I think it's useful enough that other Pythoners might want it,
so here...
| |
by: Jon Slaughter |
last post by:
I'm having a little trouble understanding what the slicing problem is.
In B.S.'s C++ PL3rdEd he says
"Becayse the Employee copy functions do not know anything about Managers,
only the Employee...
|
by: Steven Watanabe |
last post by:
I know that the standard idioms for clearing a list are:
(1) mylist =
(2) del mylist
I guess I'm not in the "slicing frame of mind", as someone put it, but
can someone explain what the...
|
by: bearophileHUGS |
last post by:
>From this interesting blog entry by Lawrence Oluyede:
http://www.oluyede.org/blog/2006/07/05/europython-day-2/
and the Py3.0 PEPs, I think the people working on Py3.0 are doing a
good job, I am...
|
by: John Henry |
last post by:
If I have a list of say, 10 elements and I need to slice it into
irregular size list, I would have to create a bunch of temporary
variables and then regroup them afterwords, like:
# Just for...
|
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,...
|
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...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |