       Re: Replacing list elements while retaining structure

• To: mathgroup at smc.vnet.net
• Subject: [mg74320] Re: Replacing list elements while retaining structure
• Date: Sun, 18 Mar 2007 00:51:44 -0500 (EST)
• References: <esdj70\$olc\$1@smc.vnet.net><esls3d\$pvn\$1@smc.vnet.net>

```Thanks to everyone who responded to this, and apologies for not
following up sooner- finals, etc.  After playing around with some of
the suggestions offered here and on the other thread that Ray Koopman
pointed out, I did get something working that I'm happy with, and
wanted to post it just in case anyone else is interested.  Here we go:

ClearAll[partitionedAs];
partitionedAs[A_List, B_List] :=
With[{B2 = B /. Thread[Flatten[B] -> Null]},

ReplacePart[B2, Flatten[A],
Table[{i}, {i, 1, Length[Flatten[A]]}]
]

] /; Length[Flatten[A]] == Length[Flatten[B]]

The above function sequentially replaces all the elements of B with
the elements of A.  None of the elements need to be atomic, and in
either list there may be repeated elements, infinite elements,
elements which themselves involve operations on lists (i.e., Dot), and
so on; these cases should all be handled as expected.

As both Andrzej Kozlowski and Peter Pein pointed out, the first step
needs to be replacing all of the elements in B with some atomic
expression so that we can match them easily in the Position function.
This can be accomplished by building a list of replacement rules that
take each element of B to Null and applying these rules to B; the
resulting list is called B2 above.  That's the only real trick here;
the rest of it is straightforward.

I tested all of the odd cases that occurred to me, but I'm sure I've
overlooked something; if anyone finds an example that gives an
unexpected result I'd be interested in seeing it.

Here are some examples that do work:

In:=
Range~partitionedAs~{{{{{a,b,c},{{d,e}}}},f,h,i,{j},k}}

Out=
{{{{{1,2,3},{{4,5}}}},6,7,8,{9},10}}

In:=
\!\({{{1\/Gamma[1\/b, z]\^3}, {{2}, 3}, 4\ x\^2},
{5}}~partitionedAs~{x, \
{{Gamma[1\/x]\^\(5\/16\) - eleventyTwo}, =E2=88=9E, x . {i, j, k}, x^2}}\)

Out=
\!\({1\/Gamma[1\/b, z]\^3, {{2}, 3, 4\ x\^2, 5}}\)

Comments would be welcome, and thanks once again to everyone for the
suggestions; they were all very helpful!

-Daniel

On Mar 7, 3:11=C2=A0am, "Ray Koopman" <koop... at sfu.ca> wrote:
> See the thread "Insulating data from code" that ran May 16-23, 2006:
>
>
> > Okay, so I have two lists, X and Y. =C2=A0X has some complex structure
> > which is important, but the values contained in X are not important.
> > Y has values that are important, but the structure of Y is not. =C2=A0I=
was
> > to sequentially replace all the elements in X with the elements of Y.
>
> > It's assumed that X and Y have the same total number of elements, i.e.
> > Length[Flatten[X]] == Length[Flatten[Y]]
>
> > Here is an example:
>
> > In:=
> > structurize[{x,x,{{x},x},x},{1,2,3,4,5}]
>
> > Out=
> > {1,2,{{3},4},5}
>
> > This is what I have so far:
>
> > structurize[X_List, Y_List] := ReplacePart[X, Flatten[Y], Position[X,
> > a_ /; \
> > =C2=A0 =C2=A0 =C2=A0 Table[{i}, {i, 1, Length[
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Flatten[Y]]}]] /; Length[Flatten[X]] ===
Length[Flatten[Y]]
>
> > This works fine so long as the elements of X are atomic expressions;
> > however, if there is an element of X which is a more complicated
> > expression, like x^2, then this function does not work as desired
> > because the pattern in Position[] matches x^2 as well as x and 2. =C2=
=A0Is
> > there a way to avoid matching parts of a subexpression? =C2=A0Is there a
> > better way to approach the problem from the get-go?
>