Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从numpy数组中删除_Python_Arrays_Numpy - Fatal编程技术网

Python 从numpy数组中删除

Python 从numpy数组中删除,python,arrays,numpy,Python,Arrays,Numpy,我有两个NumPy数组 a = np.array([]) b = np.array([]) 这些数组在整个代码中使用内聚值填充。但是现在我想从两个数组中删除元素,其中a中的值大于数字5 我猜是这样的 a = a[~a>5] 但是我不知道如何删除数组b中索引完全相同的元素。您可以使用np.extract选择特定元素并重新分配到数组: >>> x = np.arange(10) >>> x=np.extract(x<5,x) >>>

我有两个NumPy数组

a = np.array([])
b = np.array([])
这些数组在整个代码中使用内聚值填充。但是现在我想从两个数组中删除元素,其中a中的值大于数字5

我猜是这样的

a = a[~a>5]
但是我不知道如何删除数组b中索引完全相同的元素。

您可以使用np.extract选择特定元素并重新分配到数组:

>>> x = np.arange(10)
>>> x=np.extract(x<5,x)
>>> x
array([0, 1, 2, 3, 4])
numpy.extractcondition,arr

返回满足某些条件的数组元素

您还可以对此类任务使用索引:

>>> x = np.array([3,4,7,11,0,34,6,1,3,4,2])
>>> x[x<5]
array([3, 4, 0, 1, 3, 4, 2])
>>> np.extract(x<5,x)
array([3, 4, 0, 1, 3, 4, 2])
您可以设置索引ix=a