我有一个numpy
数组arr = np.arange(20).reshape(2,10)
。我想定义切片sli = slice(start,stop, step)
,然后再访问arr[0,sli]
。如何定义sli
以使arr[0,sli]
成为整个数组?即sli=:
我已经尝试过了:
sli = slice(0,-1) # equivalent to arr[0,:-1]
sli = slice(0,0) # equivalent to []
sli = slice(0) # equivalent to []
sli = slice(0,10) # works fine, but i might not know the shape of arr
# at the time I define sli
答案 0 :(得分:2)
使用Ellipsis
表示法:
import numpy as np
arr = np.arange(20).reshape(2,10)
sli = ...
out = arr[0, sli]
print(out)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
适用于python3.x。对于旧版本,您应该使用slice(None)
。
答案 1 :(得分:1)
评论JonClements
http://example.com/my_app/class/3/