I've got a list with row matrices:
rows = [ matrix([[1, 0, 0]]), matrix([[0, 1, 0]]), matrix([[0, 0, 1]]) ]
and I attempted to loop over these using for (a, b, c) in rows:
, but instead of this working, I got an error:
ValueError: not enough values to unpack (expected 3, got 1)
The expected behaviour would be to unpack the three elements in the row to a, b, c
:
for (a, b, c) in rows:
print(f"{a} {b} {c}")
> 1 0 0
> 0 1 0
> 0 0 1
Unfortunately, this would work on [1, 0, 0]
, but not on [[1, 0, 0]]
.
I realized this is because they're [[doubly packed]]
, but I was wondering if there was a simple solution to this issue?
Since, we are using
a,b,c
to extract elements from each matrix's first row, it seems we are guaranteed to have 3 elements per row and it would be one per row for each of those matrices. So, one simple solution (as asked) would be to use the array constructor when using the loop and squeeze out the singleton nested level, like so -Or simpler
np.squeeze()
-Note that this won't be the most memory efficient way but works as a simple way to extract those required scalar values.
Here's a sample run -