Re: Applying "Replace" to subsets of lists

• To: mathgroup at smc.vnet.net
• Subject: [mg129194] Re: Applying "Replace" to subsets of lists
• From: Ray Koopman <koopman at sfu.ca>
• Date: Thu, 20 Dec 2012 03:25:11 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-newout@smc.vnet.net
• Delivered-to: mathgroup-newsend@smc.vnet.net
• References: <kas2tl\$cgt\$1@smc.vnet.net>

On Dec 19, 1:55 am, abed.aln... 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 doesn't work (I'm not sure why):
> In: {{b, 2}, b} /. {{x_, y_}, f_[b]} -> {{x, y}, 1, f[1]}
> Out: {{b, 2}, b}
>
> 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}
>
> Using the 'levelspec' argument of 'Replace' also fails since 'b' can appear in different forms:
> In: Replace[{{b, 2}, b^2}, b -> 1, 1]
> Out: {{b, 2}, b^2}
> In: Replace[{{b, 2}, b^2}, b -> 1, 2]
> Out: {{1, 2}, 1}
>
> Thank you,
>
> Abed

MapAt[# /. b -> 1 &, {{b, 2}, b^2}, 2]

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