# Replace elements of a list based on a prefix

I have this list;

a={{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}}

and I would like to do a replace based on the prefix of each element of the list. For example, if the prefix is {0} I would like to modify to this

a={1,1,1,1,0,0,0,0}

or if the prefix is {1,0} the list would become;

a={0,0,0,1,1,0,0,0}

I'd need it to be general so that if the elements and prefix size changes it will still work. Also, I worked on this all day and got nowhere due to my procedural programming background. Thanks.

a={{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}};
Cases[a, {b_, _, _} -> If[b == 0, 1, 0]]
Cases[a, {b_, c_, _} -> If[{b,c} =={1,0}, 1, 0]]


For changeable length, use ___ in place of _, like {a_,b_,___} This would do just fine. Simply replace the rules to make it match other patterns

• Thanks, it works. So what if the elements of "a" are variable length, e.g., {0,0,0,0,0}, would I have to modify the Case patterns {b_,._,._} to {b_,._,._,._,._} ? Jun 4, 2018 at 4:08
• @user58558 You could use BlankSequence (I.e. __) e.g. {b_, __} to match sequences of arbitrary length. Jun 4, 2018 at 5:16
• @MarcoB BlankSequence worked. Thanks for the tip. Jun 4, 2018 at 6:02

Update: f4 and f5 should work as is in version 8. Variants of f1, f2 and f3 that avoid the functions and forms that are only available in later versions are:

ClearAll[f1b, f2b, f3b]
f1b[l_List, {p__}] := Boole[MatchQ[#, {p, ___}] & /@ l]
f2b[l_List, p_List] := Boole[MatchQ[#[[;; Min[Length@#, Length@p]]], p] & /@ l]
f3b[l_List, p_List] := Boole[Equal[#[[;; Min[Length@#, Length@p]]], p] & /@ l]

b = {{0}, {1, 0, 1, 1, 1, 1, 1}, {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1},
{1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1}};
f1b[b, {0}]


{1, 0, 1, 1, 1, 1, 0, 0, 0, 0}

f1b[b, {1, 0}]


{0, 1, 0, 0, 0, 0, 1, 1, 0, 0}

Equal @@ (#[b, {0}] & /@ {f1b, f2b, f3b})


True

ClearAll[f1, f2, f3, f4, f5]
f1[l_List, {p__}] := Boole[MatchQ[{p, ___}] /@ l]
f2[l_List, p_List] := Boole[MatchQ[p] /@ l[[All, ;; UpTo@Length@p]]]
f3[l_List, p_List] := Boole[EqualTo[p] /@ l[[All, ;; UpTo @ Length@p]]]
f4[a_, {p__}] := Block[{f}, f[{p, ___}] = 1; f[_] := 0; f /@ a];
f5[a_, {p__}] := Replace[a, {{p, ___} :> 1, {__} :> 0}, 1]

f1[a, {0}]


{1, 1, 1, 1, 0, 0, 0, 0}

f1[a, {1, 0}]


{0, 0, 0, 0, 1, 1, 0, 0}

 f5[a, {0}] == f4[a, {0}] == f3[a, {0}] == f2[a, {0}] == f1[a, {0}]


True

f3[a, {1, 0}] == f2[a, {1, 0}] == f1[a, {1, 0}]


True

b= {{0}, {1, 0, 1, 1, 1, 1, 1}, {0, 0, 0}, {0, 0, 1}, {0, 1, 0},
{0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1,1, 0}, {1, 1, 1}} ;

f1[b, {0}]


{1, 0, 1, 1, 1, 1, 0, 0, 0, 0}

f1[b, {0}] == f2[b, {0}] == f3[b, {0}] == f4[b, {0}] == f5[b, {0}]


True

f1[b, {1,0}]


{0, 1, 0, 0, 0, 0, 1, 1, 0, 0}

f1[b, {1, 0}] == f2[b, {1, 0}] == f4[b, {1, 0}] == f5[b, {1, 0}]


True

• Thanks. I'm going to have to study this one. Will this still work with variable elements in "a", e.g. instead of {0,0,0} they might be {0,0,0,0,0}....etc. Jun 4, 2018 at 4:12
• @user58558, all three should work for lists with variable lengths.
– kglr
Jun 4, 2018 at 4:24
• Doesn't run on my computer, I have version 8 if that matters. f1 & f2 generate error that MatchQ requires two inputs. Also, EqualTo and UpTo not default commands or not defined. Jun 4, 2018 at 5:28
• @user58558, the operator form MathQ[form] of MathcQ is new in versions 10+. It is equivalent to MatchQ[#, form]&. EqualTo is also new in version 10.3. I will update with versions that should work in version 8.
– kglr
Jun 4, 2018 at 5:42
• rev b functions work in version 8, thanks. In first code block f1[b, {0}] should be f1b[b,{0}] and perhaps elsewhere in your update. Jun 5, 2018 at 3:10