6
$\begingroup$

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.

$\endgroup$

2 Answers 2

5
$\begingroup$
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

$\endgroup$
3
  • $\begingroup$ 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_,._,._,._,._} ? $\endgroup$
    – user58558
    Jun 4, 2018 at 4:08
  • $\begingroup$ @user58558 You could use BlankSequence (I.e. __) e.g. {b_, __} to match sequences of arbitrary length. $\endgroup$
    – MarcoB
    Jun 4, 2018 at 5:16
  • $\begingroup$ @MarcoB BlankSequence worked. Thanks for the tip. $\endgroup$
    – user58558
    Jun 4, 2018 at 6:02
3
$\begingroup$

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

Original answer:

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

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – user58558
    Jun 4, 2018 at 4:12
  • $\begingroup$ @user58558, all three should work for lists with variable lengths. $\endgroup$
    – kglr
    Jun 4, 2018 at 4:24
  • $\begingroup$ 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. $\endgroup$
    – user58558
    Jun 4, 2018 at 5:28
  • $\begingroup$ @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. $\endgroup$
    – kglr
    Jun 4, 2018 at 5:42
  • $\begingroup$ 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. $\endgroup$
    – user58558
    Jun 5, 2018 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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