I'm sure this has been asked before, but I can't find the right combination of search keywords to find it. Apologies.
I want to do a ReplaceAll
on a list of lists, for example, I would like:
{{a, b}, {c, d}} /. {i_, j_} -> {i, j, 0}
What I want is to turn this list into {{a,b,0}, {c,d,0}}
, but I can't figure out a way to tell Mathematica to only look at lists that are one level "below". Running the above returns {{a, b}, {c, d}, 0}
, as i
matched {a,b}
and j
matched {c,d}
.
How do I pattern match lists that nested?
Replace
with the correct level specification or narrow your pattern, perhaps by using the contents of the list:{i_Integer, j_Integer}
or something like that if possible. I would post an answer, but I'm very positive this is a duplicate... Either way, hope that helped. $\endgroup$Replace
is the way to go, but you could also map aReplaceAll
as in#/.{_,_}:>...&/@list
. Use:>
instead of->
so that it works even wheni
andj
have a value $\endgroup$VectorQ[]
:list /. v_?VectorQ -> PadRight[v, 1]
. $\endgroup$