Re: Replacing elements in a list

• To: mathgroup at smc.vnet.net
• Subject: [mg90117] Re: Replacing elements in a list
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sun, 29 Jun 2008 05:37:07 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <g451tk\$ppr\$1@smc.vnet.net>

```yedidel at gmail.com wrote:
> Hello,
>
> I have a list of pairs that looks like this:
>
> {{1,2},{3,4},{1,{1,2}}}
>
> Each pair element can be a pair by itself.
>
> I have another list which lists all elements possible:
> {1,2,3,4,{1,2}}
>
> Now I want to create a list of rules that will take the element list
> and give it a running number ({1,2} will be 5 in this example)
>
> after that I want to apply those rules to the pair list BUT that will
> do it only from the second level down. This means that the final
> result should be:
>
> {{1,2},{3,4},{1,5}}
> without the first pair {1,2} changing to 5 because it is a pair and
> not an element.

Assuming I have correctly interpreted your specifications,

Replace[list1,
Cases[list2,
x : {_, _} :> (x -> Sequence @@ Flatten@Position[list2, x])], {2}]

should do what you wish. For instance,

list1 = {{1, 2}, {3, 4}, {1, {1, 2}}};
list2 = {1, 2, 3, 4, {1, 2}};
Replace[list1,
Cases[list2,
x : {_, _} :> (x -> Sequence @@ Flatten@Position[list2, x])], {2}]

{{1, 2}, {3, 4}, {1, 5}}

Regards,
-- Jean-Marc

```

• Prev by Date: Re: One thing in two ways, with different result.
• Next by Date: Re: One thing in two ways, with different result.
• Previous by thread: Re: Replacing elements in a list
• Next by thread: Interpolation scattered data in Mathematica 6