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


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

Re: Applying "Replace" to subsets of lists

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Applying "Replace" to subsets of lists
Date 2012-12-20 08:20 +0000
Message-ID <kauhoc$irm$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


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}

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web