I want to delete the line in python.

Asked 2 years ago, Updated 2 years ago, 38 views

I have a question about python.
What kind of code should I write to delete a line that has a value of 0 for all elements?
For example,

import numpy as np
a=np.array([0,0,0],[1,1,1],[2,2,2],[0,0,0],[3,3,3]])

If so, I would like to delete the first and fourth lines of a.

python python3

2022-09-29 21:29

2 Answers

a[a.any(axis=1),:]

Wouldn't it be all right if


2022-09-29 21:29

If you want to filter arrays that do not contain or contain zeroes, but are not just zeroes, you can use the array does not contain zeroes.

import numpy as np
a=np.array([0,0,0],[1,1,1],[2,2,2],[0,0,0],[3,3,3],[0,1,2,3]])
a=filter(lambdax:not 0 in x or len(set(x))>1,a)

Output Results

[[1,1,1], [2,2,2], [3,3,3], [0,1,2,3]]


2022-09-29 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.