Skip to main content
added a variant
Source Link
Syed
  • 59.5k
  • 5
  • 40
  • 95
Clear["Global`*"];
list = {m, i, s, s, i, s, s, i, p, p, i};

Count[list[[1 ;; #]], list[[#]]] & /@ Range@Length@list

Length /@ (DeleteCases[list[[1 ;; #]], Except[list[[#]]]] & /@ 
   Range@Length@list)

MapIndexed[Count[list[[1 ;; First@#2]], #1] &, list]

ReplaceList[list, {x__, y___} -> {x}] // Map[Count[#, Last@#] &]

Length /@ (PositionIndex[list[[1 ;; #]]][list[[#]]] & /@ 
   Range@Length@list)

TakeList[list, {#}] & /@ Range@Length@list // Map[First] // 
 Map[Count[#, Last@#] &] 

Reverse@ReplaceList[
  Reverse@list, {___, x_, y___} :> Count @@ {{x, y}, x}]

Result:

{1, 1, 1, 2, 2, 3, 4, 3, 1, 2, 4}

Clear["Global`*"];
list = {m, i, s, s, i, s, s, i, p, p, i};

Count[list[[1 ;; #]], list[[#]]] & /@ Range@Length@list

Length /@ (DeleteCases[list[[1 ;; #]], Except[list[[#]]]] & /@ 
   Range@Length@list)

MapIndexed[Count[list[[1 ;; First@#2]], #1] &, list]

ReplaceList[list, {x__, y___} -> {x}] // Map[Count[#, Last@#] &]

Length /@ (PositionIndex[list[[1 ;; #]]][list[[#]]] & /@ 
   Range@Length@list)

TakeList[list, {#}] & /@ Range@Length@list // Map[First] // 
 Map[Count[#, Last@#] &]

Result:

{1, 1, 1, 2, 2, 3, 4, 3, 1, 2, 4}

Clear["Global`*"];
list = {m, i, s, s, i, s, s, i, p, p, i};

Count[list[[1 ;; #]], list[[#]]] & /@ Range@Length@list

Length /@ (DeleteCases[list[[1 ;; #]], Except[list[[#]]]] & /@ 
   Range@Length@list)

MapIndexed[Count[list[[1 ;; First@#2]], #1] &, list]

ReplaceList[list, {x__, y___} -> {x}] // Map[Count[#, Last@#] &]

Length /@ (PositionIndex[list[[1 ;; #]]][list[[#]]] & /@ 
   Range@Length@list)

TakeList[list, {#}] & /@ Range@Length@list // Map[First] // 
 Map[Count[#, Last@#] &] 

Reverse@ReplaceList[
  Reverse@list, {___, x_, y___} :> Count @@ {{x, y}, x}]

Result:

{1, 1, 1, 2, 2, 3, 4, 3, 1, 2, 4}

Source Link
Syed
  • 59.5k
  • 5
  • 40
  • 95

Clear["Global`*"];
list = {m, i, s, s, i, s, s, i, p, p, i};

Count[list[[1 ;; #]], list[[#]]] & /@ Range@Length@list

Length /@ (DeleteCases[list[[1 ;; #]], Except[list[[#]]]] & /@ 
   Range@Length@list)

MapIndexed[Count[list[[1 ;; First@#2]], #1] &, list]

ReplaceList[list, {x__, y___} -> {x}] // Map[Count[#, Last@#] &]

Length /@ (PositionIndex[list[[1 ;; #]]][list[[#]]] & /@ 
   Range@Length@list)

TakeList[list, {#}] & /@ Range@Length@list // Map[First] // 
 Map[Count[#, Last@#] &]

Result:

{1, 1, 1, 2, 2, 3, 4, 3, 1, 2, 4}