What does [:, :] mean on NumPy arrays

前端 未结 3 1150
梦谈多话
梦谈多话 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 00:56

    numpy uses tuples as indexes. In this case, this is a detailed slice assignment.

    [0]     #means line 0 of your matrix
    [(0,0)] #means cell at 0,0 of your matrix
    [0:1]   #means lines 0 to 1 excluded of your matrix
    [:1]    #excluding the first value means all lines until line 1 excluded
    [1:]    #excluding the last param mean all lines starting form line 1 
             included
    [:]     #excluding both means all lines
    [::2]   #the addition of a second ':' is the sampling. (1 item every 2)
    [::]    #exluding it means a sampling of 1
    [:,:]   #simply uses a tuple (a single , represents an empty tuple) instead 
             of an index.
    

    It is equivalent to the simpler

    self.activity[:] = self.h
    

    (which also works for regular lists as well)

提交回复
热议问题