python - Numpy 数组 : NOT select specific rows or columns

标签 python numpy numpy-slicing

我有一个简单的 numpy 数组。我想选择除第一行和第六行之外的所有行 我尝试过:

temp = np.array([1,2,3,4,5,6,7,8,9])
t = temp[~[0,5]]

我收到以下错误:

 TypeError: bad operand type for unary ~: 'list'

正确的做法是什么?

最佳答案

您可以使用numpy.delete删除特定索引位置的元素:

t = np.delete(temp, [0, 5])

或者您可以创建一个 bool 数组,然后可以否定索引:

bool_idx = np.zeros(len(temp), dtype=bool)
bool_idx[[0, 5]] = True
t = temp[~bool_idx]

关于python - Numpy 数组 : NOT select specific rows or columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664085/

相关文章:

python - 在 python 中快速访问/查询大的分隔文本文件

python - 将数组轴滚动不同维度的量(概括跨步索引滚动)

python - Google App Engine 中有哪些 python 包/库?

python - 递归生成所有位数和为 n 的 k 位数字

Python 命令 print() 不打印

python - 使用没有循环的numpy在python中用数组或向量填充数组

python - 在 Kivy 中导入库

python - Numpy ":"运营商广播问题

python - 如何将 MultiIndex 转换并 reshape 为 3D Numpy 数组?

python - 在 3-D numpy 数组的小切片上高效使用 1-D pyfftw