2

I have the following code :

import numpy as np
x=np.array([[3, 5, 1]])
print(x.shape) #get (1,3)
np.multiply(x.shape, 8) #get [ 8, 24]

print(*x.shape) # get 1 3
np.array((np.multiply(*x.shape), 8)) #get [3, 8]

Please explain why/how np.multiply(*x.shape, 8) get [3, 8] ?

1
  • What are you trying to accomplish here? x,shape is just a tuple. Why multiply it? Commented Dec 1, 2018 at 17:36

2 Answers 2

1

What is happening is that by doing

np.multiply(*x.shape)

You are unpacking the tuple (1,3) using the * operator, and passing each element as an argument to np.multiply. So that results in 1*3 which is 3.

Then, you are just wrapping the result of that up into an array with 8, so you end up with an array that is [3, 8]

Sign up to request clarification or add additional context in comments.

Comments

0

The * unpacks iterables. So if x.shape is (1,3) and you call np.multiply(*x.shape) you will actually call np.multiply(1,3) which gives 3. The 8 is just hardcoded, so nothing special there.

Also, because you wrote it: the 8 is not an argument of np.multiply here.

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.