What does [:, :] mean on NumPy arrays

前端 未结 3 1146
梦谈多话
梦谈多话 2020-12-13 00:12

Sorry for the stupid question. I\'m programming on PHP but found some nice code on Python and want to \"recreate\" it on PHP. But I\'m quite frustrated about the line

<
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 01:09

    The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension.

    a = numpy.zeros((3, 3))
    
    In [132]: a
    Out[132]: 
    array([[ 0.,  0.,  0.],
           [ 0.,  0.,  0.],
           [ 0.,  0.,  0.]])
    

    Assigning to second row:

    In [133]: a[1, :] = 3
    
    In [134]: a
    Out[134]: 
    array([[ 0.,  0.,  0.],
           [ 3.,  3.,  3.],
           [ 0.,  0.,  0.]])
    

    Assigning to second column:

    In [135]: a[:, 1] = 4
    
    In [136]: a
    Out[136]: 
    array([[ 0.,  4.,  0.],
           [ 3.,  4.,  3.],
           [ 0.,  4.,  0.]])
    

    Assigning to all:

    In [137]: a[:] = 10
    
    In [138]: a
    Out[138]: 
    array([[ 10.,  10.,  10.],
           [ 10.,  10.,  10.],
           [ 10.,  10.,  10.]])
    

提交回复
热议问题