我想以正确的方式做到这一点:
data = np.array(data)
data =[
[1, 1, 2, 1],
[0, 1, 3, 2],
[0, 2, 3, 2],
[2, 4, 3, 1],
[0, 2, 1, 4],
[3, 1, 4, 1]]
这应该成为(删除以0开头的行):
[1, 1, 2, 1]
[2, 4, 3, 1]
[3, 1, 4, 1]
到目前为止,我这样做了:
lines = []
for i in range(0, len(data[0])):
if data[0,i] != 0:
lines.append(data[:,i])
lines = np.array(lines)
然后我找到了这个好方法:
mask = 1 <= data[0,:]
现在我想将该掩码应用于该数组。该掩码显示:[True, False, False, True, False, True]
。我该怎么做?
答案 0 :(得分:2)
为什么不呢:
[ar for ar in data if ar[0] != 0]
这假设数组不为空。
答案 1 :(得分:1)
I presume you have a numpy array based on the data[0,:]
and data[0,i]
you have in your question and you mean data[:, 0]
:
import numpy as np
data = np.array([
[1, 1, 2, 1],
[0, 1, 3, 2],
[0, 2, 3, 2],
[2, 4, 3, 1],
[0, 2, 1, 4],
[3, 1, 4, 1]])
data = data[data[:,0] != 0]
print(data)
Output:
[[1 1 2 1]
[2 4 3 1]
[3 1 4 1]]
data[0,:]
is the first row [1 1 2 1]
not the first column
答案 2 :(得分:0)
使用列表理解
In [56]: [elem for elem in data if elem[0] !=0]
Out[56]: [[1, 1, 2, 1], [2, 4, 3, 1], [3, 1, 4, 1]]