如何在其中包含一个零numpy的阵列中删除行? [英] How do I delete a row in a numpy array which contains a zero?
本文介绍了如何在其中包含一个零numpy的阵列中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个函数来删除它在零值的所有行。
这不是从我的code,但我使用的想法的一个例子:
导入numpy的是NP
一个= np.array((7,1,2,8],[4,0,3,2],[5,8,3,6],[4,3,2,0]))
B = []因为我在范围内(LEN(A)):
对于j的范围(LEN(一个由[i])):
如果[I] [J] == 0:
在b.append㈠打印'B =',B
您在B zero_row:
X = np.delete(一,zero_row,0)打印'A =',一
这是我的输出:
B = [1,3]
一个= [[7 1 2 8]
[4 0 3 2]
[5 8 3 6]
[4 3 2 0]]
我如何与B中的指数摆脱了行?
对不起,我是相当新的任何帮助,这将是非常美联社preciated。
解决方案
我试图写一个函数来删除它在零值的所有行。
块引用>您不必编写为一个函数,它可以在一个单一的前pression来完成:
>>> A [np.all(A!= 0,轴= 1)]
阵列([7,1,2,8],
[5,8,3,6]])读作:从
A
是所有行完全是非零I'm trying to write a function to delete all rows in which have a zero value in. This is not from my code, but an example of the idea I am using:
import numpy as np a=np.array(([7,1,2,8],[4,0,3,2],[5,8,3,6],[4,3,2,0])) b=[] for i in range(len(a)): for j in range (len(a[i])): if a[i][j]==0: b.append(i) print 'b=', b for zero_row in b: x=np.delete(a,zero_row, 0) print 'a=',a
and this is my output:
b= [1, 3] a= [[7 1 2 8] [4 0 3 2] [5 8 3 6] [4 3 2 0]]
How do I get rid of the rows with the index in b? Sorry, I'm fairly new to this any help would be really appreciated.
解决方案I'm trying to write a function to delete all rows in which have a zero value in.
You don't need to write a function for that, it can be done in a single expression:
>>> a[np.all(a != 0, axis=1)] array([[7, 1, 2, 8], [5, 8, 3, 6]])
Read as: select from
a
all rows that are entirely non-zero.这篇关于如何在其中包含一个零numpy的阵列中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文