I have three lists:

a = {{{"I282", "I284", "S285", "D286", "T287", "P288"}}, 
     {{"N393", "S394", "V395", "I396", "E397", "K398"}}, 
     {{"K136", "A151", "A156", "G157", "A158", "K159"}}}

b = {{"A278", "G279", "S280", "G281", "I282", "I283", "I284", "S285", 
      "D286", "T287", "P288", "V289", "H290", "D291", "C292"}, 
     {"A384", "I385", "D386", "E387", "I388", "T389", "N390", "K391", 
      "V392", "N393", "S394", "V395", "I396", "E397", "K398"}, 
     {"A158", "K159", "S160", "F161", "Y162", "K163", "N164", "L165", 
      "I166", "W167", "L168", "V169", "K170", "K171", "G172"}}


If any element in each sublist in b is in the corresponding sublist in a, I need to bold the corresponding letter in c. All three lists' indices are associated, and their lengths are the same. The lengths of the sublists of b are equal to the lengths of the strings in c. The prefacing letters are the same.

For example, look at the first sublist of b which is {"A278", "G279", "S280", "G281", "I282", "I283", "I284", "S285", "D286", "T287", "P288", "V289", "H290", "D291", "C292"}. The output should be AGSGIIISDTPVHDC.

I'm struggling with this, because I'm not familiar with traversing three lists at once, and it's problematic working with the indices of letters in the strings in c.

I guess if you can find a way, we don't really need c. We could just StringTake the letters of b.

  • 4
    $\begingroup$ "traversing three lists" - MapThread[] is the function for situations like that. As I only have gedanken Mathematica, please try this yourself: MapThread[Function[{ak, bk, ck}, Row[MapThread[If[MemberQ[ak[[1]], #1], Style[#2, Bold], #2] &, {bk, Characters[ck]}]]], {a, b, c}] $\endgroup$ Oct 14, 2018 at 5:54
  • $\begingroup$ It does the job, thank you so much @J.M.issomewhatokay. Noted about MapThread[]. If you want to add your comment as an answer I'll mark it correct. $\endgroup$
    – brienna
    Oct 14, 2018 at 6:24

1 Answer 1

f = StringJoin @ MapAt[ToString[Style[#, Red, Bold], StandardForm] &, #, Position @ ##2]&;
f @@@ Transpose[{StringTake[#, 1] & /@ b, b, Alternatives @@@ First /@ a}]

enter image description here


MapThread[f, {StringTake[#, 1] & /@ b, b, Alternatives @@@ First /@ a}]

same result


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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