Store Slice Index as Object

后端 未结 1 633
无人共我
无人共我 2021-01-22 03:30

Say I have a list of lists of lists etc... of some depth:

ExampleNestedObject = numpy.ones(shape = (3,3,3,3,3))

In general I can get an element

相关标签:
1条回答
  • 2021-01-22 04:14

    The trick is to think of an index object as a tuple of slice objects.

    Example1:

    Object[1,2,:] == Object[(1,2,slice(None,None,None))]
    

    Example2:

    WantedSliceObject = (1,2,slice(None,None,None), 4,5)
    Object[1,2,:,4,5] == Object[WantedSliceObject]
    

    Note the syntax of '''slice:

    #slice(start, stop[, step])
    
    #1 ==  slice(1, 2, 1)
    
    WantedSliceObject2 = (
       slice(1, 2, 1),
       slice(2, 2, 1),
       slice(None,None,None), 
       slice(4, 2, 1),
       slice(5, 2, 1)
       )
    
    #WantedSliceObject2 == WantedSliceObject
    
    0 讨论(0)
提交回复
热议问题