有什么方法可以删除numpy数组“就地"数组中的特定元素吗?在python中: [英] Is there any way to delete the specific elements of an numpy array "In-place" in python:
本文介绍了有什么方法可以删除numpy数组“就地"数组中的特定元素吗?在python中:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当调用"np.delete()"时,我不希望为减小尺寸的数组定义一个新变量.我想对原始的numpy数组执行删除.任何想法?
when calling the "np.delete()", I am not interested to define a new variable for the reduced size array. I want to execute the delete on the original numpy array. Any thought?
>>> arr = np.array([[1,2], [5,6], [9,10]])
>>> arr
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
>>> np.delete(arr, 1, 0)
array([[ 1, 2],
[ 9, 10]])
>>> arr
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
but I want:
>>> arr
array([[ 1, 2],
[ 9, 10]])
推荐答案
NumPy数组是固定大小的,因此不能有np.delete
的就地版本.任何此类函数都必须更改数组的大小.
NumPy arrays are fixed-size, so there can't be an in-place version of np.delete
. Any such function would have to change the array's size.
您能获得的最接近的结果是重新分配arr
变量:
The closest you can get is reassigning the arr
variable:
arr = numpy.delete(arr, 1, 0)
这篇关于有什么方法可以删除numpy数组“就地"数组中的特定元素吗?在python中:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文