Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Please consider the following list:

data={1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3};

Now I would like to split the list into 0-sequences and Not-0-sequences as following:

{{1}, {0, 0, 0}, {2, 5, 2, 3}, {0, 0}, {3}}

All numbers are non-negative Integers, if that helps.

share|improve this question
    
Not quite the same, but related –  Leonid Shifrin Jun 13 '12 at 8:55

4 Answers 4

up vote 21 down vote accepted
Split[{1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}, Xor[#1 != 0, #2 == 0] &]

or more compactly

Split[{1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}, Xnor[#1 == 0, #2 == 0] &]

works nicely here. Another way is:

SplitBy[{1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}, Unitize]

or

SplitBy[{1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}, Sign]

or, as Szabolcs suggests:

SplitBy[{1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}, # == 0 &]
share|improve this answer
1  
It's probably good to note here that PossibleZeroQ does some symbolic processing. It's not "simple inside". While using it works, for a simple programming task (on a list of strictly integers) I'd feel more comfortable with Unitize or just # == 0 &. Why not include Unitize in the answer instead? –  Szabolcs Jun 13 '12 at 7:32
    
Done. $\phantom{}$ –  J. M. Jun 13 '12 at 7:35
    
@Mr. Wizard, not at all, but it seems so easy to offend you... ;P –  J. M. Jun 13 '12 at 12:37
    
@J.M. Offend? No, I just have trouble reading/seeing things sometimes. –  Mr.Wizard Jun 13 '12 at 13:04
1  
@Mr. Wizard: all styles have their place. On my part, Mathematica has a lot of two-argument functions, but I'd only consider infixing a handful of them... –  J. M. Jun 13 '12 at 13:21

Here's a recursive rule based solution for fun:

Clear[f]
f[l_List] := l /. {x__?(# != 0 &) | Longest[x : 0 ..], y___} :> {{x}, Sequence @@ f[{y}]}
f[{x_}] := {{x}}

f[{1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}]    
(* {{1}, {0, 0, 0}, {2, 5, 2, 3}, {0, 0}, {3}} *)
share|improve this answer
1  
Impressive! ... Any easy question in Mma is a seed for very good answers like this one –  belisarius Jun 13 '12 at 1:10
1  
+1 for elegance, although note that, while recursion hides it, this will suffer from the same performance hit as if it would be explicitly based on ReplaceRepeated, for large lists. –  Leonid Shifrin Jun 13 '12 at 8:56

I recently read The Little Schemer by Friedman and Felleisen, so the following construction suggests itself:

split[{}]={};
split[{Longest[x__?(#>0&)] | Longest[x__?(#==0&)], y___}]:=Join[{{x}},split[{y}]];

In[34]:= split[{1,0,0,0,2,5,2,3,0,0,3}]
Out[34]= {{1},{0,0,0},{2,5,2,3},{0,0},{3}}

I make no claim that this is at all practical.

share|improve this answer
    
Scheme was the language for the intro-CS course when I was an undergrad. I liked it, but it drove the people who already knew C nuts. –  Brett Champion Jun 13 '12 at 1:53
    
@BrettChampion At one point I made a list of things I liked about Mathematica, and it included the fact that everything is an expression. Lisp popped up when I researched the roots of this feature. On a related note, that book really did teach me to write recursions! (Always ask null?, etc.) –  Michael Wijaya Jun 13 '12 at 2:07

This isn't going to win any awards for efficiency but I had fun putting it together.

f[{h_, t__}] := f[{h}, {t}]

f[x___, {a_, b___}, {h_, t__}] := 
  If[
     (h == 0) == (a == 0),
     f[x, {a, b, h}, {t}],
     f[x, {a, b}, {h}, {t}]
  ]

f[x___, y : {_}] := {x, y}

f @ {1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3}
{{1}, {0, 0, 0}, {2, 5, 2, 3}, {0, 0}, {3}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.