Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Slicing" in Python Expressions documentation

Tags:

I don't understand the following part of the Python docs:

http://docs.python.org/reference/expressions.html#slicings

Is this referring to list slicing ( x=[1,2,3,4]; x[0:2] )..? Particularly the parts referring to ellipsis..

slice_item       ::=  expression | proper_slice | ellipsis 

The conversion of a slice item that is an expression is that expression. The conversion of an ellipsis slice item is the built-in Ellipsis object.

like image 780
dbr Avatar asked Apr 15 '09 16:04

dbr


People also ask

What is a slicing expression in Python?

Slicing in Python is a feature that enables accessing parts of sequences like strings, tuples, and lists. You can also use them to modify or delete the items of mutable sequences such as lists. Slices can also be applied on third-party objects like NumPy arrays, as well as Pandas series and data frames.

What is the syntax of slicing?

Python slice() Function Syntax: start: Starting index where the slicing of object starts. stop: Ending index where the slicing of object stops. step: It is an optional argument that determines the increment between each index for slicing.

How does slicing work in Python list?

With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list. If Lst is a list, then the above expression returns the portion of the list from index Initial to index End, at a step size IndexJump.

What is slicing in Python explain list and string with slicing?

Slicing Strings You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.


1 Answers

Ellipsis is used mainly by the numeric python extension, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. eg, given a 4x4 array, the top left area would be defined by the slice "[:2,:2]"

>>> a array([[ 1,  2,  3,  4],        [ 5,  6,  7,  8],        [ 9, 10, 11, 12],        [13, 14, 15, 16]])  >>> a[:2,:2]  # top left array([[1, 2],        [5, 6]]) 

Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:] for dimensions not specified, so for a 3d array, a[...,0] is the same as a[:,:,0] and for 4d, a[:,:,:,0].

Note that the actual Ellipsis literal (...) is not usable outside the slice syntax in python2, though there is a builtin Ellipsis object. This is what is meant by "The conversion of an ellipsis slice item is the built-in Ellipsis object." ie. "a[...]" is effectively sugar for "a[Ellipsis]". In python3, ... denotes Ellipsis anywhere, so you can write:

>>> ... Ellipsis 

If you're not using numpy, you can pretty much ignore all mention of Ellipsis. None of the builtin types use it, so really all you have to care about is that lists get passed a single slice object, that contains "start","stop" and "step" members. ie:

l[start:stop:step]   # proper_slice syntax from the docs you quote. 

is equivalent to calling:

l.__getitem__(slice(start, stop, step)) 
like image 67
Brian Avatar answered Oct 24 '22 04:10

Brian