Skip to main content
added 276 characters in body
Source Link
E. Chan-López
  • 31.1k
  • 3
  • 29
  • 50
data = {{0, 5678}, {0, 5654}, {1, 87675},
       {2, 243}, {2, 257}, {1, 9790}, 
       {0, 7688}, {1, 97779}, {0, 6858}};

Using SequencePosition and Extract:

f = Extract[#1, List /@ Union @@ SequencePosition[#1, {{#2, _}, {#3, _}}]] &;

f[data, 0, 1]

{{0, 5654}, {1, 87675}, {0, 7688}, {1, 97779}}

f[data, 1, 2]

{{1, 87675}, {2, 243}}

Or using Partition and Pick:

f[l_, {m_, n_}] := 
Join @@ Pick[#, #[[1, 1]] == m && #[[2, 1]] == n & /@ #] &@Partition[l, 2, 1]

f[data, {0, 1}]

{{0, 5654}, {1, 87675}, {0, 7688}, {1, 97779}}

f[data, {1, 2}]

{{1, 87675}, {2, 243}}

data = {{0, 5678}, {0, 5654}, {1, 87675},
       {2, 243}, {2, 257}, {1, 9790}, 
       {0, 7688}, {1, 97779}, {0, 6858}};

Using SequencePosition and Extract:

f = Extract[#1, List /@ Union @@ SequencePosition[#1, {{#2, _}, {#3, _}}]] &;

f[data, 0, 1]

{{0, 5654}, {1, 87675}, {0, 7688}, {1, 97779}}

f[data, 1, 2]

{{1, 87675}, {2, 243}}

data = {{0, 5678}, {0, 5654}, {1, 87675},
       {2, 243}, {2, 257}, {1, 9790}, 
       {0, 7688}, {1, 97779}, {0, 6858}};

Using SequencePosition and Extract:

f = Extract[#1, List /@ Union @@ SequencePosition[#1, {{#2, _}, {#3, _}}]] &;

f[data, 0, 1]

{{0, 5654}, {1, 87675}, {0, 7688}, {1, 97779}}

f[data, 1, 2]

{{1, 87675}, {2, 243}}

Or using Partition and Pick:

f[l_, {m_, n_}] := 
Join @@ Pick[#, #[[1, 1]] == m && #[[2, 1]] == n & /@ #] &@Partition[l, 2, 1]

f[data, {0, 1}]

{{0, 5654}, {1, 87675}, {0, 7688}, {1, 97779}}

f[data, {1, 2}]

{{1, 87675}, {2, 243}}

Source Link
E. Chan-López
  • 31.1k
  • 3
  • 29
  • 50

data = {{0, 5678}, {0, 5654}, {1, 87675},
       {2, 243}, {2, 257}, {1, 9790}, 
       {0, 7688}, {1, 97779}, {0, 6858}};

Using SequencePosition and Extract:

f = Extract[#1, List /@ Union @@ SequencePosition[#1, {{#2, _}, {#3, _}}]] &;

f[data, 0, 1]

{{0, 5654}, {1, 87675}, {0, 7688}, {1, 97779}}

f[data, 1, 2]

{{1, 87675}, {2, 243}}