Dynamic Python Array Slicing

后端 未结 2 662
一生所求
一生所求 2021-01-13 21:55

I am facing a situation where I have a VERY large numpy.ndarray (really, it\'s an hdf5 dataset) that I need to find a subset of quickly because they entire arra

2条回答
  •  一生所求
    2021-01-13 22:41

    Okay, I finally found an answer just as someone else did.

    Suppose I have array:

    my_array[...]
    >array(
      [[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],
    
       [[10, 11, 12],
        [13, 14, 15],
        [16, 17, 18]]])
    

    I can use the slice object, which apparently is a thing:

    sl1 = slice( None )
    sl2 = slice( 1,2 )
    sl3 = slice( None )
    ad_array.matrix[(sl1, sl2, sl3)]
    >array(
      [[[ 4,  5,  6]],
    
       [[13, 14, 15]]])
    

提交回复
热议问题