1

I have the following piece of code:

p1 = np.array([[[[[[[[[[0.]*2]*2]*2]*2]*2]*2]*2]*2]*2]*2)
s = [0]*10
#
# Do something with s
#
p1[s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9]] += 1

Is there a smarter way of:

  1. Creating p1 without all those brackets, and
  2. Access the components of p1 with a string or a list?

I have in mind something like:

p1[s] += 1

or:

p1[*s] += 1

For example, what if instead of 10 indices I wanted N indices?

0

1 Answer 1

7
np.array([[[[[[[[[[0.]*2]*2]*2]*2]*2]*2]*2]*2]*2]*2)

is better written as:

np.zeros((2,2,2,2,2,2,2,2,2,2))

Or as there are ten 2s:

np.zeros((2,)*10)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! Do you also know anything about the second problem?
For the second problem, with this solution or any other, just use p1[s]. Just make sure that s is of the right type, e.g., a tuple here.

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.