I am writing a program that will have an arbitrary number of :
and None
in arbitrary locations of an n-dimensional NumPy array. Therefore, I want a way to unpack these :
and None
axis operators into the []
that indexes an array and auto-populates certain axes according to where the :
and None
are. According to Pylance:
Unpack operator in subscript requires Python 3.11 or newerPylance
However, while using Python 3.11, I get the following error:
Traceback (most recent call last):
File "/home/.../quant.py", line 261, in <module>
print(arr[*lhs_axes] + arr2[None,None,:])
~~~^^^^^^^^^^^
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Current code:
import numpy as np
if __name__ == "__main__":
lhs_ind, rhs_ind = 'ij', 'k'
lhs_axes = [':' for i in lhs_ind]
lhs_axes.append(None)
arr1 = np.ones((2,2))
arr2 = np.ones(2)
print(arr1[*lhs_axes] + arr2[None,None,:])