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


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

Applying "Replace" to subsets of lists

Started byabed.alnaif@gmail.com
First post2012-12-19 09:55 +0000
Last post2012-12-20 08:20 +0000
Articles 2 — 2 participants

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


Contents

  Applying "Replace" to subsets of lists abed.alnaif@gmail.com - 2012-12-19 09:55 +0000
    Re: Applying "Replace" to subsets of lists Ray Koopman <koopman@sfu.ca> - 2012-12-20 08:20 +0000

#12870 — Applying "Replace" to subsets of lists

Fromabed.alnaif@gmail.com
Date2012-12-19 09:55 +0000
SubjectApplying "Replace" to subsets of lists
Message-ID<kas2tl$cgt$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#12884

FromRay Koopman <koopman@sfu.ca>
Date2012-12-20 08:20 +0000
Message-ID<kauhoo$irq$1@smc.vnet.net>
In reply to#12870
On Dec 19, 1:55 am, abed.aln...@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]

[toc] | [prev] | [standalone]


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


csiph-web