删除所有行和列numpy.ndarray中的最后一项 [英] delete last item in all rows and columns numpy.ndarray
问题描述
我正在尝试删除numpy.ndarray
(type = class numpy.ndarray
)中行和列中的最后一项.我的数组有30行和180列(即每行180个值).我已经尝试过numpy.delete
,但这只是删除了整个行/列.
I am trying to delete the last item in both the rows and columns in my numpy.ndarray
(type = class numpy.ndarray
). My array has 30 rows and 180 columns (i.e. 180 values per row). I have tried numpy.delete
but this simply removes the whole row/column.
为了说明我要实现的目标,我在Python中使用and数组创建了以下示例,并嵌套了for循环:
To illustrate what I want to achieve I created the following example in Python using and array and nested for loops:
a = np.array([[[1,2,3,4,5,6],[1,2,3,4],[1,2,3,4]],[[1,2,3,4,5,6],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]]])
for list in a:
for sublist in list:
del sublist[-1]
使用
print(a)
给出以下数组:
[[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] [[1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4]] [[1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4]] [[1, 2, 3, 4], [1, 2, 3, 4]]]
使用
print(list)
for循环后给出:
[[1, 2, 3, 4, 5], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3, 4, 5], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
[[1, 2, 3], [1, 2, 3]]
不幸的是,在我的阵列上使用它会产生以下错误:
Unfortunately using this on my array gives the following error:
TypeError:"numpy.float64"对象不支持删除项目
TypeError: 'numpy.float64' object does not support item deletion
谢谢
更新:
我从网格NetCDF文件中提取我的信息.由于list
是Python关键字,因此我已将单词list
更改为l
.这对我来说并没有改变.
Update:
I am extracting my information from a grid NetCDF file. I have changed the word list
to l
since list
is a Python keyword. This didn't change it for me.
这提供了我的数组的一个很好的例子:
This provides a good example of my array:
c = np.arange(5400).reshape(30,180)
for l in c:
for i in l:
del i[-1]
运行此代码时,出现以下错误:
When I run this code I get the following error:
Traceback (most recent call last): File "main.py", line 18, in <module>
del i[-1]
TypeError: 'numpy.int64' object does not support item deletion
推荐答案
del i[-1]
是列表操作. np.array
不支持.
del i[-1]
is a list operation. np.array
does not support that.
计数特定值的出现并同时将其删除展示了删除列表和数组之间的区别.
Count the occurrences of a specific value and remove them at the same time demonstrates the differences between lists and arrays when it comes to deletion.
您的示例a
是对象dtype,包含列表
Your example a
is object dtype, containing lists
In [111]: a.shape
Out[111]: (11,)
In [112]: [len(i) for i in a]
Out[112]: [3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2]
In [113]: a[0]
Out[113]: [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4], [1, 2, 3, 4]]
a[0]
是一个3元素列表,带有不同长度的子列表.
a[0]
is a 3 element list, with sublists of different length.
尚不清楚您要删除什么.删除a
中的元素,或a
中每个元素的元素,或这些元素的子列表中的元素.
It's not clear what you want to delete. Delete elements from a
, or elements from each element of a
, or elements from the sublists of those elements.
此外,如果实际数据来自NetCDF
,则它实际上可能是多维数组.或者,如果对象是dtype,则元素本身可能是(2d)数组.
Furthermore, if the real data is from NetCDF
it might actually a multidimensional array. Or if object dtype, the elements might themselves be (2d) arrays.
以防万一,切片是从数组中删除行/列的正确方法:
In case, slicing is the right way to remove rows/columns from an array:
In [114]: a = np.arange(12).reshape(3,4)
In [115]: a
Out[115]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [116]: a[:-1, :-1]
Out[116]:
array([[0, 1, 2],
[4, 5, 6]])
结果为view
;它不会更改a
本身. a = a[:-1, :-1].copy()
是创建尺寸减小的数组而又不保留任何原始数组的最干净的方法.
The result is a view
; it does not change a
itself. a = a[:-1, :-1].copy()
is the cleanest way to creates a reduced size array without leaving the any of the original around.
这篇关于删除所有行和列numpy.ndarray中的最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!