473,795 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(sl c, n):
start, stop, step = slc.indices(n)
if cmp(stop-start,0)<>cmp(s tep,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(sl ice(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 7253
sm****@gmail.co m 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.co m 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
2609
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 rules written down somewhere. Thanks,
29
3411
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 wonder why it is not implemented for tuples. George
0
946
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) .... >>> hotcat
5
2905
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 indices for that container. So I need to generate 3 unique numbers from integer range . There seems to be no simple and efficient way to do this. Any implementation I have come up with involves maintaining a list of
3
2120
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 the sequence. But what are the corresponding indices?
5
1701
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 gets you the 3rd through the 3rd through the 5th character of the string because indexing starts at 0 and you get everything up to, but not including the second index.
1
1401
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, Hey Presto! no more tediously typing a 1-9 seq into your interpreter and then getting a slice just to check what you get.. (Yes you. You know you do that !) ... Cheers Steve x = '0123456789' x 0123456789 x
31
1737
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 first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example: +---+---+---+---+---+ | H | e | l | p | A |
5
3838
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 show various countries appearing to be depressed when "moused-over". To keep it simple at first, I just decided to try two countries. After copying the HTML and JavaScript codes generated by ImageReady into the page I wanted to insert the map into,...
0
9522
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,...
0
10443
Oralloy
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...
0
10216
jinu1996
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...
1
10165
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,...
0
6783
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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 we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.