The following is the slicings syntax that I copied from The Python Language Reference:
slicing ::= primary "[" slice_list "]"
slice_list ::= slice_item ("," slice_item)* [","]
slice_item ::= expression | proper_slice
proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ]
lower_bound ::= expression
upper_bound ::= expression
stride ::= expression
Per my understanding, this syntax equates to SomeMappingObj[slice_item,slice_item etc...]
which again equates to something like a[0:2:1,4:7:1]
and a =[i for i in range(20)]
.
But, I can't test this in IPython and I did not find any questions about multiple slicings. Is my interpretation about multiple slicing in python correct? What am I doing incorrectly?
In [442]: a=[i for i in range(20)]
In [443]: a[0:12:2]
Out[443]: [0, 2, 4, 6, 8, 10]
In [444]: a[0:12:2,14:17:1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-444-117395d33bfd> in <module>()
----> 1 a[0:12:2,14:17:1]
TypeError: list indices must be integers or slices, not tuple
This reference manual describes the syntax and “core semantics” of the language. It is terse, but attempts to be exact and complete. The semantics of non-essential built-in object types and of the built-in functions and modules are described in The Python Standard Library.
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.
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.
A slice_list
should contain as many "dimensions" as the object being indexed. The multi-dimensional capability is not used by any Python library object that I am aware of, but you can test it easily with numpy
:
import numpy as np
a = np.array([[1, 2], [3, 4]])
a[0:1, 0]
There are a number of such features in the Python language that are not used directly in the main library. The __matmul__
magic method (@
operator) is another example.
Note that the grammar is structured this way to allow two things:
A family of slice
literals:
x:y:z == slice(x, y, z)
x:y == slice(x, y, None)
x: == slice(x, None, None)
x::z == slice(x, None, z)
::z == slice(None, None, z)
:y:z == slice(None, y, z)
:: == slice(None, None, None)
:y: == slice(None, y, None)
There are a few other patterns possible (x:y:
, :y
, etc), but each
is a variation on one of the above.
Slice literals may only be used inside [...]
, not in any arbitrary expression.
Otherwise, the comma-separate list is treated like any other tuple. When you write an expression like f[1, 2:3, 5::7]
, then f.__getitem__
receives a tuple (1, slice(2, 3, None), slice(5, None, 7)
as its argument. What f.__getitem__
does with that argument is entirely up to type(f)
's implementation of __getitem__
. For instance, lists and strings only accept int
and slice
values as arguments, and dicts only accept hashable values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With