Message Boards Message Boards

0
|
7713 Views
|
9 Replies
|
3 Total Likes
View groups...
Share
Share this post:

List to list transformation rules

Posted 10 years ago

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
9 Replies

Here is a solution I built from the help I got in another thread. Hope it is useful. I thought it was!

It allows you to create a list by filtering for both included items and excluded items. You can remove all items from one list from a second list and also exclude items from a third list.

bigList = {"item1", "item2", "item3", "item4", "item6", "item7", 
   "item8", "item9"};
    excludedElements = {"item1", "item2", "item5", "item6"};
    includedElements = {"item8", "item9"};

Select[Select[bigList, 
    StringFreeQ[#, excludedElements] &],
     ! StringFreeQ[#, includedElements] &]

Out[14]={"item8", "item9"}
POSTED BY: David Johnston

What about just removing all items from one list from another list?

list1 = {"item1", "item2", "item3", "item4"}
list2 = {"item1", "item2", "item5", "item6"}

here are my poor guesses:

ReplaceAll[list1,list2]

or

DeleteCases[list1, list2]

expected result:

Out[165]= {item3, item4}
POSTED BY: David Johnston
Posted 10 years 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
Posted 10 years ago

Or if there's no transformation at all:

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

{a, b, c}
POSTED BY: Douglas Kubler
Posted 10 years 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

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
Posted 10 years ago

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

POSTED BY: Andre Hautot

Another approach

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

Out[12]= {a, b, c}
POSTED BY: Frank Kampas
In[11]:= Inner[ReplaceAll, {x, x, x}, {x -> a, x -> b, x -> c}, List]

Out[11]= {a, b, c}
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract