Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12882 > unrolled thread

Re: Applying "Replace" to subsets of lists

Started byBill Rowe <readnews@sbcglobal.net>
First post2012-12-20 08:20 +0000
Last post2012-12-20 08:20 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Applying "Replace" to subsets of lists Bill Rowe <readnews@sbcglobal.net> - 2012-12-20 08:20 +0000

#12882 — Re: Applying "Replace" to subsets of lists

FromBill Rowe <readnews@sbcglobal.net>
Date2012-12-20 08:20 +0000
SubjectRe: Applying "Replace" to subsets of lists
Message-ID<kauhoc$irm$1@smc.vnet.net>
On 12/19/12 at 4:55 AM, abed.alnaif@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}

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web