Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have the list

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

I would like to make sublists of everything beginning with the {a} element till the next {a} or the and of the list.

Either

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

or

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

would be acceptable. I'm sure there is a duplicate, but I can't find the easiest way to do this (that isn't a clunky While loop with an AppendTo)

share|improve this question
1  
Related: (2952), (3412), (23607), (47296), (77796) – Mr.Wizard Jan 15 at 15:04
1  
@Kuba - in the actual job I'm doing, I'm trying to get molecule geometries from a large text file where they are always preceded by a line that is like "----------------------". I can probably do it better with some combination of grep and awk, but I can never figure out awk. – JasonB Jan 15 at 15:17
1  
@JasonB Ok, I've adjusted the wording to take that into account :) – Kuba Jan 15 at 15:21
up vote 16 down vote accepted
Split[list, (#2 =!= {a}) &]
{
 {{a}, {0, 2, 5}, {5, 4, 1}},
 {{a}, {1, 1, 0}, {1, 4, 2}, {3, 3, 0}}, 
 {{a}, {3, 2, 0}, {1, 4, 1}}
}

If you add Map@Rest you will get the first form.

share|improve this answer
    
This is great. Can you tell me what the practical difference is between != and =!= are? Either one seems to give the same result for your code. Perhaps when only comparing two things there is no difference? Or it's similar to the difference between SameQ and Equal – JasonB Jan 15 at 14:20
    
Answered my question there, via this blog post: "If you are not sure if someone gives a variable or a value you can use =!= to force a true or false" – JasonB Jan 15 at 14:24
    
@JasonB :) so you know. But that's not so easy, maybe you want 1 to be 1. then == gives true and === false. – Kuba Jan 15 at 14:29
Rest /@ Internal`PartitionRagged[list, 
   Flatten@Differences@Position[Append[list, {a}], {a}]] // Column

enter image description here

share|improve this answer
    
Nice, I was trying something like Span@Partition@Position..... but I like PartitionRagged better. Where are all these undocumented functions listed? Are they in a staging area for the next version? :-) – JasonB Jan 15 at 15:34
    
SplitBy[l, FreeQ[{##}, a] &] /. {{a}} :> Sequence[]

Or

Level[Nest[
   MapAt[TakeDrop[#, First@FirstPosition[#, {a}]] &, 
     Level[#, {-3}], -1] &, {l}, 
   Length[Position[l, {a}]]], {-3}] //. {a} | {} -> Sequence[]
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.