Why doesnt the python slice syntax wrap around from negative to positive indices?

2024/9/28 9:20:16

I noticed, given l = [1,2,3], that l[-1:] returns [3] as expected, but that l[-1:0] returns [], very much unlike what I expected. I then tried [-1:1], which I expected to return [3,1], but it also returns [].

Is there a good reason why the slice syntax does not wrap around from negative to positive indices (and the other way round)?

It seems it would be pretty useful and pretty straightforward to implement, but maybe I'm missing something.

Answer

Slicing has a very simple definition: you go from start by jumps of step as long as you are below stop. If start or step are negative, first add the length of the array.

One time your suggestion causes irksome behaviour is:

x[10:-10]

If x[-10] is after x[10], you want the slice from x[10] to x[len(x)-10-1]. If you have wrap-around, you'll have the slice x[10:] + x[:-10], which is mostly useless.

Wrap-around behaviour is easy to emulate (eg x[m:] + x[:n]) with the current behaviour, but the current, more useful, behavior is hard to emulate with wrap-around.

https://en.xdnf.cn/q/71357.html

Related Q&A

How do I modify a generator in Python?

Is there a common interface in Python that I could derive from to modify behavior of a generator?For example, I want to modify an existing generator to insert some values in the stream and remove some…

Reading a website with asyncore

I would like to read a website asynchronously, which isnt possible with urllib as far as I know. Now I tried reading with with plain sockets, but HTTP is giving me hell. I run into all kind of funky en…

How to select specific the cipher while sending request via python request module

Usecase: I want to find out how many ciphers are supported by the hostname with python request module.I am not able to find a way to provide the cipher name to request module hook. Can anyone suggest …

Different classes made by type with the same name in Python?

I was playing around with metaclasses in Python and found something very curious. I can create two classes with the same name, but that are actually different objects. See:>>> def create_class…

Installing python server for emacs-jedi

I am trying to install Jedi for emacs using marmalade package manager by following instructions here -- http://tkf.github.io/emacs-jedi/latest/. The package manger installs Jedi along with its dependen…

Multi-feature causal CNN - Keras implementation

Im currently using a basic LSTM to make regression predictions and I would like to implement a causal CNN as it should be computationally more efficient.Im struggling to figure out how to reshape my cu…

Adding a join to an SQL Alchemy expression that already has a select_from()

Note: this is a question about SQL Alchemys expression language not the ORMSQL Alchemy is fine for adding WHERE or HAVING clauses to an existing query:q = select([bmt_gene.c.id]).select_from(bmt_gene) …

How should I move blobs from BlobStore over to Google Cloud Storage?

Our application has been running on App Engine using the Blobstore for years. We would like to move our video files over to Google Cloud Storage. What is the best practice for migrating large blobs f…

Python: Find `sys.argv` before the `sys` module is loaded

I want to find the command line arguments that my program was called with, i.e. sys.argv, but I want to do that before Python makes sys.argv available. This is because Im running code in usercustomize.…

Dotted lines instead of a missing value in matplotlib

I have an array of some data, where some of the values are missingy = np.array([np.NAN, 45, 23, np.NAN, 5, 14, 22, np.NAN, np.NAN, 18, 23])When I plot it, I have these NANs missing (which is expected)f…