加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 从数组数组中提取数组

发布时间:2020-12-20 11:59:52 所属栏目:Python 来源:网络整理
导读:我有这个数组: arr = np.array([[[ -1.,-1.,0.,0.],[ 0.1,0.1,2.,3.,4.]],# -- this one [[ -1.,-1.],16.,17.,0.1]],# -- and this one [[ -1.,4.,5.,6.]],# -- and this one [[ 0.,[ 1.,1.,2.]],9.,10.,11.]]]) # -- and the last one 我想在每个数组中提
我有这个数组:

arr = np.array([[[ -1.,-1.,0.,0.],[ 0.1,0.1,2.,3.,4.]],# <-- this one

               [[ -1.,-1.],16.,17.,0.1]],# <-- and this one

               [[ -1.,4.,5.,6.]],# <-- and this one

               [[  0.,[  1.,1.,2.]],9.,10.,11.]]]) # <-- and the last one

我想在每个数组中提取第二个数组,结果如下:

res = [[ 0.1,4.],0.1],6.],2.],11.]]

我希望在一行代码中获得res,我试过这个,但它没有用

arr[:][1] # select the element 1 in each array
# I got
array([[ -1.,-1. ],[  0.1,0.1]])

有谁能解释为什么?

我找到的唯一解决方案是明确指出每个索引(arr [0] [1] …),我不喜欢.

解决方法

这是一个3D数组,您正在尝试选择第二个轴的第二个元素并沿其余轴提取所有元素.所以,它很简单 –

arr[:,1,:]

我们可以跳过列出:对于尾轴,所以它进一步简化为 –

arr[:,1]

样品运行 –

In [360]: arr
Out[360]: 
array([[[ -1.,0. ],4. ]],[[ -1.,6. ]],[[  0.,2. ]],11. ]]])

In [361]: arr[:,1]
Out[361]: 
array([[  0.1,4. ],6. ],2. ],11. ]])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读