简体   繁体   English

删除所有行和列numpy.ndarray中的最后一项

[英]delete last item in all rows and columns numpy.ndarray

I am trying to delete the last item in both the rows and columns in my numpy.ndarray ( type = class numpy.ndarray ). 我正在尝试删除numpy.ndarray的行和列中的最后一项( type = class numpy.ndarray )。 My array has 30 rows and 180 columns (ie 180 values per row). 我的数组有30行和180列(即每行180个值)。 I have tried numpy.delete but this simply removes the whole row/column. 我已经尝试过numpy.delete但这只是删除了整个行/列。

To illustrate what I want to achieve I created the following example in Python using and array and nested for loops: 为了说明我要实现的目标,我在Python中使用and数组创建了以下示例,并嵌套了for循环:

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]

Using 使用

print(a) 

Gives the following 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]]] 

Using 使用

print(list)

after the for loops gives: 在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' object does not support item deletion TypeError:'numpy.float64'对象不支持项目删除

Thanks 谢谢


Update: I am extracting my information from a grid NetCDF file. 更新:我从网格NetCDF文件中提取信息。 I have changed the word list to l since list is a Python keyword. 我将单词list更改为l因为list是Python关键字。 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] is a list operation. del i[-1]是列表操作。 np.array does not support that. np.array不支持。

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. 计算特定值的出现并同时将其删除,这说明删除列表和数组之间的区别。

Your example a is object dtype, containing lists 您的示例a是对象dtype,包含列表

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] is a 3 element list, with sublists of different length. a[0]是3个元素的列表,带有不同长度的子列表。

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. 删除从元件a ,或者从元件中的每个元件a从这些元素的子列表,或元件。

Furthermore, if the real data is from NetCDF it might actually a multidimensional array. 此外,如果实际数据来自NetCDF则它实际上可能是多维数组。 Or if object dtype, the elements might themselves be (2d) arrays. 或者,如果对象为dtype,则元素本身可能是(2d)数组。

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]])

The result is a view ; 结果是一个view ; it does not change a itself. 它不会改变a本身。 a = a[:-1, :-1].copy() is the cleanest way to creates a reduced size array without leaving the any of the original around. a = a[:-1, :-1].copy()是创建尺寸减小的数组而又不保留任何原始数组的最a = a[:-1, :-1].copy()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM