5
$\begingroup$

I would like to gather elements of a list separated by a delimiter ":"

lis = {"az", "2","b",":","7","b",":","7","c"} 

to make:

lis2 = {{az, 2,b},{7,b},{7,c}}

Thanks in advance for any direction...

$\endgroup$
6
$\begingroup$

This

is = {"az", "2", "b", ":", "7", "b", ":", "7", "c"};
DeleteCases[SplitBy[lis, # == ":" &], {":"}]

returns {{"az", "2", "b"}, {"7", "b"}, {"7", "c"}}

$\endgroup$
  • 1
    $\begingroup$ SameQ[] (===) is more suitable here than Equal[]. $\endgroup$ – J. M. will be back soon Jan 28 '17 at 7:01
6
$\begingroup$

In 10th and later versions you can also use SequenceCases:

SequenceCases[lis, {a : Except[":"] ..} :> {a}]

{{"az", "2", "b"}, {"7", "b"}, {"7", "c"}}

Also:

Split[#, # =!= ":" &] /.  ":" | {":"} -> (## &[]) & @ lis

{{"az", "2", "b"}, {"7", "b"}, {"7", "c"}}

$\endgroup$

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.