7
$\begingroup$

I have a set of data (called MyList), and I need to extract every value that appears after a certain marker (in this case 1000000000). Unfortunately the number of values in between the markers are not known in advance, which means that Partition can't be used. I can't extract each value manually as there are thousands of such entries.

MyList = {100000000, 241, 282, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 280, \
2, 0, 141675, 0, 52313, 0, 60293, 0, 146917, 100000000, 234, 267, 1, \
2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 267, 0, 112814, 0, 605096, \
100000000, 202, 239, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 236, 3, \
0, 137451, 0, 14687, 0, 602723, 0, 146917, 100000000, 247, 304, 1, 2, \
3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 294, 10, 0, 103246, 0, 52313, 0, \
603875, 0, 146917, 100000000, 245, 292, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, \
10, 1, 11, 289, 3, 0, 185242, 0, 14687, 0, 606101, 0, 146917, \
100000000, 215, 266, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 265, 1, \
0, 109229, 0, 146917, 0, 605375, 0, 146917, 100000000, 243, 294, 1, \
2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 292, 2, 0, 284599, 0, 14687, 0, \
605777, 0, 146917, 100000000, 246, 285, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, \
10, 1, 11, 283, 2, 0, 249383, 0, 14687, 0, 596362, 0, 14687, \
100000000, 249, 295, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 292, 3, \
0, 13235, 0, 146917, 0, 602667, 0, 146917, 100000000, 259, 300, 1, 2, \
3, 4, 5, 6, 7, 8, 9, 1, 10, 1, 11, 299, 1, 0, 148557, 0, 14687, 0, \
60267, 0, 14687}

Any help would be hugely appreciated. Thanks!

$\endgroup$
2
  • 6
    $\begingroup$ SequenceCases[MyList, {100000000, x_} :> x]? $\endgroup$
    – kglr
    Mar 13, 2023 at 20:57
  • 1
    $\begingroup$ Can marker repeat one another ie. ...,100000000,100000000,... ? $\endgroup$ Mar 14, 2023 at 1:42

6 Answers 6

11
$\begingroup$
Pick[Rest@MyList, Most@MyList, 100000000]

(* {241, 234, 202, 247, 245, 215, 243, 246, 249, 259} *)
$\endgroup$
2
  • 1
    $\begingroup$ Serious showstopper. $\endgroup$
    – Syed
    Mar 13, 2023 at 22:35
  • $\begingroup$ @Syed Thanks. I need one of those!! $\endgroup$
    – user1066
    Mar 14, 2023 at 19:24
8
$\begingroup$
Extract[MyList, Position[MyList, 100000000] + 1]
$\endgroup$
7
$\begingroup$

Using SequenceSplit:

SequenceSplit[MyList, {100000000}] // Map[First]

Using Split:

Split[MyList, #1 == 100000000 &] /. {{_} -> Nothing} // Map@Last

{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}

$\endgroup$
4
$\begingroup$
Cases[Partition[MyList, 2, 1], {100000000, _}][[All, 2]]


(*{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}*)
$\endgroup$
4
$\begingroup$

Another way using SequencePosition and Take:

Map[Times @@ Take[list, #] &@*List@*Last]@SequencePosition[list, {100000000, _}]

(*{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}*)

Or using SequencePosition and Part:

Part[list, #[[2]]] & /@ SequencePosition[list, {100000000, _}]

(*{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}*)

Edit: As @Syed suggest, a simpler way:

list[[Last /@ SequencePosition[list, {100000000, _}]]]
$\endgroup$
1
  • 2
    $\begingroup$ Simpler: MyList[[Last /@ SequencePosition[MyList, {100000000, _}]]] $\endgroup$
    – Syed
    Mar 14, 2023 at 6:39
2
$\begingroup$

Marker:

m = 100000000;

Using ReplaceRepeated with x as placeholder

Cases[{x, a_} :> a] @
 ReplaceRepeated[list, {h___, m, a_, t___} :> {h, {x, a}, t}]

{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}

Using SubsetPosition (new in 12.1)

Extract[SubsetPosition[list, {m}] + 1] @ list

{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}

Using SequencePosition (a variant of E. Chan-López answer)

Extract[Cases[{a_, _} :> {a + 1}] @ SequencePosition[list, {m}]] @ list

{241, 234, 202, 247, 245, 215, 243, 246, 249, 259}

$\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.