我有這樣的陣列:提取陣列
arr = np.array([[[ -1., -1., -1., 0., 0., 0.],
[ 0.1, 0.1, 0.1, 2., 3., 4.]], # <-- this one
[[ -1., -1., -1., 0., 0., -1.],
[ 0.1, 0.1, 0.1, 16., 17., 0.1]], # <-- and this one
[[ -1., -1., -1., 0., 0., 0.],
[ 0.1, 0.1, 0.1, 4., 5., 6.]], # <-- and this one
[[ 0., 0., 0., -1., 0., 0.],
[ 1., 2., 3., 0.1, 1., 2.]], # <-- and this one
[[ -1., -1., 0., 0., 0., 0.],
[ 0.1, 0.1, 1., 9., 10., 11.]]]) # <-- and the last one
我想提取每個陣列中的第二陣列,並且所述結果應該是如下:
res = [[ 0.1, 0.1, 0.1, 2., 3., 4.],
[ 0.1, 0.1, 0.1, 16., 17., 0.1],
[ 0.1, 0.1, 0.1, 4., 5., 6.],
[ 1., 2., 3., 0.1, 1., 2.],
[ 0.1, 0.1, 1., 9., 10., 11.]]
欲在一行代碼得到res
,我試過,但沒有奏效
arr[:][1] # select the element 1 in each array
# I got
array([[ -1. , -1. , -1. , 0. , 0. , -1. ],
[ 0.1, 0.1, 0.1, 16. , 17. , 0.1]])
任何人都可以解釋,爲什麼?
我發現的唯一解決方案是明確指出我不喜歡的每個索引(arr[0][1]...
)。
'解析度= [x [1] for x in arr]'? – depperm