MathGroup Archive 2012

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

Search the Archive

Re: Applying "Replace" to subsets of lists


On 12/19/12 at 4:55 AM, abed.alnaif at gmail.com wrote:

>Hello, Say I have the following list, and I'd like to replace the
>second 'b' with the value 1, leaving the first b untouched:

>MagicFunction[{{b, 2}, b}] = {{b, 2}, 1}

>How do I do this? I've tried the following:

>This doesn't work since it replaces both 'b' In: {{b, 2}, b} /. b ->
>1 Out: {{1, 2}, 1}

>This works: In: {{b, 2}, b} /. {{x_, y_}, b} -> {{x, y}, 1} Out:
>{{b, 2}, 1}

>However, 'b' may appear in different forms, in which case the
>previous approach fails: In: {{b, 2}, b^2} /. {{x_, y_}, b} -> {{x,
>y}, 1} Out: {{b, 2}, b^2}

Here is one approach

In[5]:= {{b, 2}, b} /. a_ :> {First[a], Last[a] /. b -> 1}

Out[5]= {{b,2},1}

In[6]:= {{b, 2}, b - 1} /. a_ :> {First[a], Last[a] /. b -> 1}

Out[6]= {{b,2},0}

In[7]:= {{b, 2}, b^2} /. a_ :> {First[a], Last[a] /. b -> 1}

Out[7]= {{b,2},1}




  • Prev by Date: Re: options in Plot
  • Next by Date: Re: Input alias for Surd not working
  • Previous by thread: Re: Applying "Replace" to subsets of lists
  • Next by thread: Re: Applying "Replace" to subsets of lists