Group Abstract Group Abstract

Message Boards Message Boards

List to list transformation rules

GROUPS:

Hello, Here is an oversimplified list : {x,x,x} and a list of transformation rules : {x->a, x->b, x->c}. What I need : to apply the first rule to the first element, the second rule to the second element and so on. Expected result : {a,b,c}

A neat way to do this ?

Thanks in advance, Andre

POSTED BY: Andre Hautot
Answer
19 days ago
In[11]:= Inner[ReplaceAll, {x, x, x}, {x -> a, x -> b, x -> c}, List]

Out[11]= {a, b, c}
POSTED BY: Frank Kampas
Answer
19 days ago

Another approach

In[12]:= MapThread[ReplaceAll, {{x, x, x}, {x -> a, x -> b, x -> c}}]

Out[12]= {a, b, c}
POSTED BY: Frank Kampas
Answer
19 days ago

The first solution sligthly less time-consuming, Many thanks, Andre

POSTED BY: Andre Hautot
Answer
19 days ago

Hello Andre, this is a little faster approach:

In[11]:= ReplaceAll[{x, x, x}, {x__} -> { a, b, c}]

Out[11]= {a, b, c}

Here, the result is independent from the list (input) because of _ _ (blank sequence). For example:

In[12]:= ReplaceAll[{x, y, z}, {x__} -> { a, b, c}]

Out[12]= {a, b, c}
POSTED BY: Serhan Aya
Answer
17 days ago

If the oversimplified list is always of that form then a simple transformation is:

In[32]:= x /. Partition[{x -> a, x -> b, x -> c}, 1]

Out[32]= {a, b, c}
POSTED BY: Douglas Kubler
Answer
13 days ago

Or if there's no transformation at all:

{x -> a, x -> b, x -> c}[[All, 2]]

{a, b, c}
POSTED BY: Douglas Kubler
Answer
13 days ago

Hello Serhan and Douglas, Yes my question was ultimately more general : given the (say) 3-components vector example, {f[x,y,z],g[x,y,z],h[x,y,z]}, and the three sets of rules, rulef={x->uf,y->vf,z->wf}, ruleg={x->ug,y->vg,z->wg}, ruleh={x->uh,y->vh,z->wh}, to apply rulef to f[x,y,z], ruleg to g[x,y,z] and ruleh to h[x,y,z] in a single and concise way. MapThread (apparently not Inner) does the job (see Frank's post above) :

MapThread[ReplaceAll, {{f[x, y, z], g[x, y, z], h[x, y, z]}, {{x -> uf, y -> vf, z -> wf}, {x -> ug, y -> vg, z -> wg}, {x -> uh, y -> vh, z -> wh}}}]

Answer : {f[uf, vf, wf], g[ug, vg, wg], h[uh, vh, wh]}

Regards

POSTED BY: Andre Hautot
Answer
13 days ago