1

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.

1 Answer 1

1

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]]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.