Difference between single and double bracket Numpy array?

前端 未结 3 1713
予麋鹿
予麋鹿 2021-02-01 08:22

What is the difference between these two numpy objects?

import numpy as np
np.array([[0,0,0,0]])
np.array([0,0,0,0])
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 09:12

    When you defined an array with two brackets, what you were really doing was declaring an array with an array with 4 0's inside. Therefore, if you wanted to access the first zero you would be accessing your_array[0][0] while in the second array you would just be accessing your array[0]. Perhaps a better way to visualize it is

    array: [
    [0,0,0,0],
    ]
    

    vs

    array: [0,0,0,0]
    

提交回复
热议问题