Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding python slicings syntax as described in the python language reference

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
like image 383
snalubo Avatar asked Sep 20 '16 18:09

snalubo


People also ask

What is the Python language reference?

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.

What is slicing in Python explain with example?

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.


2 Answers

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.

like image 81
Mad Physicist Avatar answered Oct 17 '22 10:10

Mad Physicist


Note that the grammar is structured this way to allow two things:

  1. A family of slice literals:

    1. x:y:z == slice(x, y, z)
    2. x:y == slice(x, y, None)
    3. x: == slice(x, None, None)
    4. x::z == slice(x, None, z)
    5. ::z == slice(None, None, z)
    6. :y:z == slice(None, y, z)
    7. :: == slice(None, None, None)
    8. :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.

  2. 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.

like image 40
chepner Avatar answered Oct 17 '22 10:10

chepner