I have a list of lists which looks something like this-
{{12,1,23,.....,4},{0,0,0,.....,0},{34,67,5,.....,60},{0,0,0,.....,0}}. I want list to look something like - {{12,1,23,....,4},{34,67,5,.....,60}}. I want to remove all those lists which have zero as their only element. How can It be done?
Asked
Active
Viewed 1,371 times
5
solphy101
- 337
- 2
- 12
-
Something related can be found in this post: Finding the number of solutions to a diophantine equation – Artes Apr 15 '17 at 15:03
3 Answers
11
list = {{12, 1, 23, 4}, {0, 0, 0, 0}, {34, 67, 5, 60}, {0, 0, 0, 0}};
DeleteCases[ list , {0 ..}]
(* {{12, 1, 23, 4}, {34, 67, 5, 60}} *)
(* this also results in the same answer *)
Cases[ list , Except@{0 ..}]
(* or *)
list /. {0 ..} :> Sequence[]
(* using select *)
Select[ list , x \[Function] FreeQ[x, {0 ..}] ]
(* assuming no negative integers *)
Select[ list , Total @ # != 0 &]
gwr
- 13,452
- 2
- 47
- 78
Ali Hashmi
- 8,950
- 4
- 22
- 42
-
2caution: the last method with
Selectassumes that there are no negative integers ! – Ali Hashmi Apr 15 '17 at 14:42 -
I thought my edit helps readability. Please, roll back, if you think it does not. (+1) for a nice answer! – gwr Apr 15 '17 at 15:55
-
6
Another possibility as of Version 11:
lists = {{12, 1, 23, 4}, {0, 0, 0, 0}, {34, 67, 5, 60}, {0, 0, 0, 0}};
lists /. {0..} -> Nothing
gwr
- 13,452
- 2
- 47
- 78
4
A slight addition:
Pick[dat, Total /@ Unitize@dat, Except[0 | _List]]
Just providing some other methods of solving similar problems.
Wjx
- 9,558
- 1
- 34
- 70