2
$\begingroup$

I have the following list:

lis = {a,b,c,d,a,e,f,a,g,h,i,a,k}

and would like to arrive at the following:

res = {{b,c,d},{e,f},{g,h,i},{k}}

Doubtless already asked and answered, but even just a pointer to the previous answer would be much appreciated!

$\endgroup$
3
  • 6
    $\begingroup$ That is the answer, right? Partition on sublists beginning with a certain marker elements $\endgroup$
    – Kuba
    Oct 4, 2016 at 19:02
  • $\begingroup$ What rule determines which subsets are to be created? $\endgroup$ Oct 4, 2016 at 19:04
  • $\begingroup$ Just making sublists consisting of whatever elements follow an instance of "a" before the next "a" shows up. The proposed solutions all work. Thank you all for your prompt help! $\endgroup$
    – Suite401
    Oct 4, 2016 at 20:54

3 Answers 3

4
$\begingroup$

The following works - perhaps there is a simpler version?

Select[SplitBy[lis, # === a &], # =!= {a} &]
$\endgroup$
10
$\begingroup$
SequenceCases[{a, b, c, d, a, e, f, a, g, h, i, a, k}, {Except[a] ..}]

{{b, c, d}, {e, f}, {g, h, i}, {k}}

$\endgroup$
4
$\begingroup$

Or with DeleteCases:

DeleteCases[SplitBy[lis, # === a &], {a}]

{{b, c, d}, {e, f}, {g, h, i}, {k}}


Extended version:

DeleteCases[SplitBy[lis, # === a &], s_ /; s == {a}]
$\endgroup$
0

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