Pick elements from list "separately"
mylist = {1, 5, 7, 3, 4, 7, 2, 9, 9, 1, 10, 12, 2, 64, 34, 64};
selector = {1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1};
pick = Pick[mylist, selector, 1];
split = Length /@ Cases[Split[selector], {1 ...}]
{3, 2, 3, 2}
Internal`PartitionRagged[pick, split]
{{1, 5, 7}, {7, 2}, {9, 1, 10}, {34, 64}}
Because your selector is 0 or 1, SplitBy
can be used as follows.
Select[SplitBy[mylist*selector, Positive], #[[1]] > 0 &]
You can combine Map
, Take
, and SequencePosition
:
Map[Take[mylist, #] &,
SequencePosition[selector, {1 ..}, Overlaps -> False]]
(* {{1, 5, 7}, {7, 2}, {9, 1, 10}, {34, 64}} *)