TransWikia.com

Replacing a specific pattern of values at level 2 in a list

Mathematica Asked by aark on March 16, 2021

I’ve a list of lists,

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}}

and each nested list has the same length, What is the proper way to replace the occurances of all m* values, i.e., m12, m22, etc., with a particular value say x.

All I want is this result

tl = {{True, False, x}, {False, x, x}, {x, False, True}}`

Any help will be appreciated.

3 Answers

One rather literal (string-based) interpretation of your question:

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};

tl /. s_ /; StringTake[ToString[s], 1] == "m" :> X

{{True, False, X}, {False, X, X}, {X, False, True}}

Correct answer by Yves Klett on March 16, 2021

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};

Replace[tl, Except[True | False] :> z, {2}]

(* {{True, False, z}, {False, z, z}, {z, False, True}} *)

Answered by kglr on March 16, 2021

In this way the True and False conditions are analyzed

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}}; 
Map[If[ToString[#1] == "True" || ToString[#1] == "False", #1, 
   x] & , tl, {2}]

$left( begin{array}{ccc} text{True} & text{False} & x text{False} & x & x x & text{False} & text{True} end{array} right)$

Answered by LCarvalho on March 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP