Dynamic Python Array Slicing

后端 未结 2 663
一生所求
一生所求 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:48

    You could slice automaticaly using python's slice:

    >>> a = np.random.rand(3, 4, 5)
    >>> a[0, :, 0]
    array([ 0.48054702,  0.88728858,  0.83225113,  0.12491976])
    >>> a[(0, slice(None), 0)]
    array([ 0.48054702,  0.88728858,  0.83225113,  0.12491976])
    

    The slice method reads as slice(*start*, stop[, step]). If only one argument is passed, then it is interpreted as slice(0, stop).

    In the example above : is translated to slice(0, end) which is equivalent to slice(None).

    Other slice examples:

    :5 -> slice(5)
    1:5 -> slice(1, 5)
    1: -> slice(1, None)
    1::2 -> slice(1, None, 2)
    

提交回复
热议问题