473,382 Members | 1,329 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,382 software developers and data experts.

slice's indices() method

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 example
from clpy about this the indices for a 'None, None, -2' slice for a
range of length 10 are given as '9, -1, -2'. The problem is that these
concrete values cannot be fed back into a slice so that a slice will
extract the same elements that the 'None, None, -2' would have
extracted. That's where the problem is. It seems to me that the indices
given *from a slice object* should be able to make a round trip and and
be used as arguments for a slice object and extract the same elements
that the non-concrete (i.e. None-containin) indices would have given.
Instead, the behavior right now only allows these indices from a slice
object to be fed to a *range* object to give back the indices of the
elements that would have been extracted from the original lenght-n
object.

Here is the full example given in the clpy archives:
>>slice(None, None, -2).indices(10)
(9, -1, -2)
>>range(9, -1, -2)
[9, 7, 5, 3, 1]
>>range(10)[slice(9, -1, -2)]
[]

Notice that although the indices given, (9, -1, -2), give a valid range
of values from 9 through 1 stepping by 2, these same indices don't
extract elements from a range when they are given as an argument to the
slice function. In essense, the slice 'None, None, -2' (or '::-2') is
not interpreted the same way as the explicitly specified values of '9,
-1, -2'. This seems unecessarily obtuse: since the indices *came* from
a slice object it seems to me that they should be able to *go back
into* a slice object; 'None, None, -2' and the indices obtained for a
length-10 object should behave the same.

In reading the discussion of Aug 2005 regarding this issue, it sounded
like the intent of slice.indices() was to get indices which could be
sent to range to generate a range whose values would match the indices
produced by the original slice and that this capability was mainly to
be used for unittest-ing. If the following sort of logic were applied
instead of the current logic I think it would improve the usability of
the indices() method for slices:

def sliceIndices(slc, n):
start, stop, step = slc.indices(n)
if cmp(stop-start,0)<>cmp(step,0): #null
if step<0:
if 0 not in [start-n,stop-n]:start-=n;stop-=n
else:
if start<0 or stop<0:start+=n;stop+=n
else: #valid
if step<0: #all should be less than 0
if start>=0 or stop>=0:start-=n;stop-=n
return start, stop, step

With this logic the following result is obtained for the same indices
used in the above examples:
>>print sliceIndices(slice(None,None,-2),10)
(-1, -11, -2)
>>range(-1, -11, -2)
[-1, -3, -5, -7, -9]
>>range(10)[slice(-1, -11, -2)]
[9, 7, 5, 3, 1]

This modification of the indices will give indices which can be used in
a slice to extract the same elements from a length-n object as the
original slice or to generate a range whose elements correspond to the
indices of the elements extracted by the original slice.

I searched through the *.py scripts in the distribution library, and
the only place I found slice indices being used was in the script
testing slices...and the test were simply assertions that the
slice.indices() gave the expected tuple.

Is there any reason not to change the behavior of the indices() method
so it gives indices that can be used in range (to give indices
corresponding to elements that would be extracted by a given slice)
*and* used as arguments for slices so that the slice with the new
indices (obtained from the indices() method) would extract the same
elements as the original slice from whence they were obtained? Would
anything (except the present unittest for the indices() method) break
if this new behavior were implemented?

/chris

Oct 30 '06 #1
2 7227
sm****@gmail.com wrote:
Is there any reason not to change the behavior of the indices() method
so it gives indices that can be used in range (to give indices
corresponding to elements that would be extracted by a given slice)
*and* used as arguments for slices so that the slice with the new
indices (obtained from the indices() method) would extract the same
elements as the original slice from whence they were obtained?
and this month's "absolutely most pointless of all pointless proposals"
award goes to...

</F>

Oct 30 '06 #2

Fredrik Lundh wrote:
sm****@gmail.com wrote:
Is there any reason not to change the behavior of the indices() method
so it gives indices that can be used in range (to give indices
corresponding to elements that would be extracted by a given slice)
*and* used as arguments for slices so that the slice with the new
indices (obtained from the indices() method) would extract the same
elements as the original slice from whence they were obtained?

and this month's "absolutely most pointless of all pointless proposals"
award goes to...

</F>
I don't see the suggestion that much different than wanting the output
of a function like ord() to be able to be used again in str() to get
back a given character. There was some good conversation about the
indices method a year ago...I was hoping to get some useful feedback as
to why this might not be an improvement to the behavior of the slice's
indices() method.

Although you have not run into the need to get the specific indices
from a slice (and perhaps that's why you think it's pointless) I did
need them and got bitten by the non-round-trip-able behavior of the
indices() method...and ended up writing a workaround rather than being
able to use the built in method.

/chris

Oct 31 '06 #3

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

Similar topics

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...
29
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I...
0
by: Mark Sargent | last post by:
Hi All, playing around with the tut now. How can I get this code to remove the original instance of 'roof'.? >>> hotcat = >>> for x in hotcat: .... if x == 'roof': hotcat.insert(6,x)...
5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
3
by: GavinCrooks | last post by:
The indices method of slice doesn't seem to work quite how I would expect when reversing a sequence. For example : '43210' '43210' So a slice with a negative step (and nothing else) reverses...
5
by: Steve Bergman | last post by:
A couple of off the wall questions. It seems to me that there is usually a solid *reason* for things in Python and I'm wondering about the rationale for the way slicing works: my_string ...
1
by: meridian | last post by:
If, like me, you're always forgetting which way around your list/seq slices need to go then worry no more. Just put my handy "slice lookupper" (TM) ) on a (large!) PostIt beside your screen and,...
31
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the...
5
by: NuberSteve | last post by:
I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a world map. I basically wanted a map that would...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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.