在Numpy/Scipy中切片数组 [英] Slicing arrays in Numpy / Scipy

查看:73
本文介绍了在Numpy/Scipy中切片数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组:

I have an array like:

a = array([[1,2,3],[3,4,5],[4,5,6]])

从其中仅包含"a"的前两列的1x2数组中切出的最有效方法是什么?

What's the most efficient way to slice out a 1x2 array out of this that has only the first two columns of "a"?

array([[2,3],[4,5],[5,6]]) in this case.

推荐答案

使用a[i,j](不是a[i][j])为二维numpy数组建立索引,但是您可以对numpy数组和矩阵使用相同的切片符号在python中使用普通矩阵(只需将它们放在单个[]中):

Two dimensional numpy arrays are indexed using a[i,j] (not a[i][j]), but you can use the same slicing notation with numpy arrays and matrices as you can with ordinary matrices in python (just put them in a single []):


>>> from numpy import array
>>> a = array([[1,2,3],[3,4,5],[4,5,6]])
>>> a[:,1:]
array([[2, 3],
       [4, 5],
       [5, 6]])

这篇关于在Numpy/Scipy中切片数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆