1
$\begingroup$

If the variable "digits" contains a sequence of numbers how to count how many times each grouping of a number occurs? For instance, for number $1$ how many times it occurs once consequitivly $(...,1,...)$ how many times twice consecutively $(...,1,1,...)$, so on until all of the consecutive occurrences it has in the list.

$\endgroup$

1 Answer 1

2
$\begingroup$

SequenceCases followed by Tally or Counts:

digits = {0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1};
Counts@SequenceCases[digits, l : {1 ..} :> Length[l]]

<|2 -> 2, 1 -> 1, 3 -> 1|>

Before SequenceCases existed, contiguous sequences might have been detected like this:

Counts@Cases[Split[digits], l : {1 ..} :> Length[l]]

<|2 -> 2, 1 -> 1, 3 -> 1|>

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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