MathGroup Archive 2006

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

Search the Archive

Re: ReplaceAll applied to List


I forgot to add how you should work before...

Use Replace specifying the level.

For example


In[31]:=
Replace[{2.3, 4, {7/8}, a, b, I, {{I^2}}}, (x_)?NumberQ -> x^2, 1]
Out[31]=
{5.289999999999999, 16, {7/8}, a, b, -1, {{-1}}}

In[32]:=
Replace[{1., {2}, 3}, (x_)?NumericQ -> g, 1]
Out[32]=
{g, {2}, g}


Regards
Dimitris



Hannes Kessler wrote:
> Hello,
>
> I want to apply transformation rules to those elements of a list which
> are not lists themselves. The structure of the list is unknown in
> advance.
>
> The following simple examples illustrate my my problems with ReplaceAll
> and List:
>
> Expected result:
> {1., {2}, 3} /. x_?NumericQ :> g
> --> {g,{g},g}
>
> Unexpected result - inspite of AtomQ[{1., {2}, 3}] giving False
> {1., {2}, 3} /. x_?AtomQ :> g
> --> g[g, g[g], g]
>
> Similarly unexpected result - inspite of Not[ListQ[{1., {2}, 3}]]
> giving False
> {1., {2}, 3} /. x_?(Not[ListQ[#]] &) :> g
> --> g[g, g[g], g]
>
> Any explanations for this behavior?
> 
> Thanks in advance,
> Hannes Kessler


  • Prev by Date: Re: Please help overcome problems with integration
  • Next by Date: Re: Limit and Root Objects
  • Previous by thread: Re: ReplaceAll applied to List
  • Next by thread: Re: ReplaceAll applied to List