Reputation: 674
I have a base array base = [0,1,2,3]
which contains elements of the set {0,...,k}
(where k
is 3
in this example). I also have another array modif
which is a n
dimensional array, where n
is the number of distinct elements in base
.
I want to add one iteratively to an element of the modif
array, given by indexes of base, so if base = [0,1,2,3]
a function must add one to modif[0,1,2,3]
.
I tried doing something like
probs[b for b in base] += 1
or
probs[(b for b in base)] += 1
or even
for b in base:
sel = probs[b]
sel += 1
But the problems are that in the first and second, it is not valid syntax, and in the third, the sel is actually a copy of probs[b]
, not the same actual objects, so the change is not done in probs
.
Upvotes: 1
Views: 31
Reputation: 107347
You don't need a comprehension just convert the indices to tuple. Here is an example:
In [42]: a
Out[42]:
array([[[ 2, 2, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]])
In [43]: b
Out[43]: [0, 1, 2]
In [44]: a[tuple(b)]
Out[44]: 7
In [45]: a[tuple(b)] += 100
In [46]: a
Out[46]:
array([[[ 2, 2, 2, 3, 4],
[ 5, 6, 107, 8, 9],
[ 10, 11, 12, 13, 14]],
[[ 15, 16, 17, 18, 19],
[ 20, 21, 22, 23, 24],
[ 25, 26, 27, 28, 29]]])
Upvotes: 1