Re: Replacing list elements while retaining structure
- To: mathgroup at smc.vnet.net
- Subject: [mg74002] Re: Replacing list elements while retaining structure
- From: Peter Pein <petsie at dordos.net>
- Date: Mon, 5 Mar 2007 04:52:50 -0500 (EST)
- References: <esdj70$olc$1@smc.vnet.net>
D. Grady schrieb:
...
> This is what I have so far:
>
> structurize[X_List, Y_List] := ReplacePart[X, Flatten[Y], Position[X,
> a_ /; \
> (Head[a] =!= List), Heads -> False],
> Table[{i}, {i, 1, Length[
> 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. Is
> there a way to avoid matching parts of a subexpression? Is there a
> better way to approach the problem from the get-go?
>
> Thanks in advance!
>
>
If I understand you correctly, you've got something like this:
test = {x, x^2, {{x + y}, x/Tan[Exp[x] - 1]}, x};
and want that:
preprocess[Except[_List]] := x;
SetAttributes[preprocess, Listable];
preprocess[test]
--> {x, x, {{x}, x}, x}
hth,
Peter