我有以下代碼:import numpy as npx=np.array([[3, 5, 1]])print(x.shape) #get (1,3)np.multiply(x.shape, 8) #get [ 8, 24]print(*x.shape) # get 1 3np.array((np.multiply(*x.shape), 8)) #get [3, 8]請解釋為什么/如何 np.multiply(*x.shape, 8) 得到 [3, 8] ?
2 回答
MYYA
TA貢獻1868條經驗 獲得超4個贊
正在發生的事情是通過做
np.multiply(*x.shape)
您正在(1,3)使用*運算符解包元組,并將每個元素作為參數傳遞給np.multiply。所以結果1*3是 3。
然后,您只是將其結果包裝到一個數組中8,因此您最終會得到一個數組[3, 8]
aluckdog
TA貢獻1847條經驗 獲得超7個贊
在*解壓縮iterables。所以如果x.shape是(1,3)并且你打電話,np.multiply(*x.shape)你實際上會打電話np.multiply(1,3)給3。在8剛剛硬編碼的,所以沒有什么特別的存在。
此外,因為你寫吧:8是不是的說法np.multiply在這里。
添加回答
舉報
0/150
提交
取消
