5
$\begingroup$

Given a list

lst={0,0,0,0,0,"A1",0,0,0,"B2",0,0,"C3","D4","E5",0,0,0,"F6"}

How to split it as

{{0,0,0,0,0},{"A1",0,0,0},{"B2",0,0},{"C3"},{"D4"},{"E5",0,0,0},{"F6"}}

I want to use SequenceSplit but didn't know how to set up the right pattern.

SequenceSplit[lst, {_String} -> {}]

Thanks!

$\endgroup$
5
$\begingroup$
Split[lst, Not @ StringQ @ #2 &]

{{0, 0, 0, 0, 0}, {"A1", 0, 0, 0}, {"B2", 0, 0}, {"C3"}, {"D4"}, {"E5", 0, 0, 0}, {"F6"}}

SequenceSplit[lst, {s_String, a : Except[_String] ...} :> {s, a}]

{{0, 0, 0, 0, 0}, {"A1", 0, 0, 0}, {"B2", 0, 0}, {"C3"}, {"D4"}, {"E5", 0, 0, 0}, {"F6"}}

$\endgroup$
  • $\begingroup$ Very nice,Thanks! what do #1 and #2 mean in Split function? $\endgroup$ – Jerry Jan 13 at 3:49
  • 2
    $\begingroup$ @Jerry see Working with Pure Functions and Slot $\endgroup$ – Mr.Wizard Jan 13 at 3:56
  • $\begingroup$ kglr, do you have something against !? :^) $\endgroup$ – Mr.Wizard Jan 13 at 3:57
  • $\begingroup$ @Mr.Wizard, thank you. $\endgroup$ – kglr Jan 13 at 4:01
  • 2
    $\begingroup$ @Jerry, in a test function used as the second argument of Split, #1 refers to the first element of a consecutive pair and #2 to the second, See also animation - Split which illustrates how it works for the default test function SameQ. $\endgroup$ – kglr Jan 13 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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