I often find I have something like this:
cur = [0, 0] # the indices into array
matrix = [[1,1,1]]
where I do
matrix[cur[0]][cur[1]]
Is there any sort of unpacking syntax here? Like:
matrix[*cur]
I often find I have something like this:
cur = [0, 0] # the indices into array
matrix = [[1,1,1]]
where I do
matrix[cur[0]][cur[1]]
Is there any sort of unpacking syntax here? Like:
matrix[*cur]
If you switch to NumPy and you're using Python 3.11+, then yes, that works.
import numpy as np
cur = [0, 0]
matrix = np.array([[1, 2, 3]])
print(matrix[*cur]) # -> 1
Before Python 3.11, you can just convert to tuple:
print(matrix[tuple(cur)]) # -> 1
Based on the name matrix
, NumPy might be a better solution in other ways too. For example you get elementwise operations.
Note: The Python 3.11 syntax change doesn't seem to be documented in the normal places (what's new and grammar). From a quick look, I only found it mentioned under typing.Unpack
: "*
couldn’t be used in certain places". It is covered in PEP 646 though, which introduced typing.TypeVarTuple
.