MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Replacing list elements while retaining structure

  • To: mathgroup at
  • Subject: [mg74320] Re: Replacing list elements while retaining structure
  • From: "D. Grady" <D.C.Grady at>
  • Date: Sun, 18 Mar 2007 00:51:44 -0500 (EST)
  • References: <esdj70$olc$><esls3d$pvn$>

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:

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

      ReplacePart[B2, Flatten[A],
        Position[B2, _?AtomQ, Heads -> False],
        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:



\!\({{{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}}\)

\!\({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!


On Mar 7, 3:11=C2=A0am, "Ray Koopman" <koop... at> wrote:
> See the thread "Insulating data from code" that ran May 16-23, 2006:
> D. Grady wrote:
> > 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=
> > 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[8]:=
> > structurize[{x,x,{{x},x},x},{1,2,3,4,5}]
> > Out[8]=
> > {1,2,{{3},4},5}
> > This is what I have so far:
> > structurize[X_List, Y_List] := ReplacePart[X, Flatten[Y], Position[X,
> > a_ /; \
> > (Head[a] =!= List), Heads -> False],
> > =C2=A0 =C2=A0 =C2=A0 Table[{i}, {i, 1, Length[
> > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Flatten[Y]]}]] /; Length[Flatten[X]] ===
> > 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=
> > there a way to avoid matching parts of a subexpression? =C2=A0Is there a
> > better way to approach the problem from the get-go?
> > Thanks in advance!

  • Prev by Date: Re: Possible bug in NSolve[equation, variable, precission]
  • Next by Date: RE: Integrate
  • Previous by thread: Re: Replacing list elements while retaining structure
  • Next by thread: Copy-paste vector graphics