1
$\begingroup$

This question already has an answer here:

I have a list consisting of strings and integers:

{"abc", 3, 4, "def", 5, 6, 7, "xz", 8, 3}

and I would like to obtain:

{{"abc", 3, 4}, {"def", 5, 6, 7}, {"xz", 8, 3}}

The general problem is how to create sublists where the first element of each is a string, and the subsequent elements are numbers until the next element is a string, at which point the this string serves as the first element of the next sublist.

I would be grateful for any guidance.

$\endgroup$

marked as duplicate by Kuba list-manipulation Aug 25 '16 at 19:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

Marked as a duplicate but let me leave this CW for clarity

list  = {"abc", 3, 4, "def", 5, 6, 7, "xz", 8, 3};

Split[list, ! StringQ[#2] &]

or (V10.2+)

SequenceCases[list, {_String, __?NumberQ}]
$\endgroup$

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