Re: List element replacement.
- To: mathgroup at smc.vnet.net
- Subject: [mg52011] Re: [mg51980] List element replacement.
- From: Tomas Garza <tgarza01 at prodigy.net.mx>
- Date: Sun, 7 Nov 2004 01:03:28 -0500 (EST)
- References: <200411060707.CAA25986@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Are the three lists the same Length? If that should be the case, e.g.,
In[1]:=
initial = {0, 3, 4, 6, 8};
compare = {0, 0, 0, 3, 8};
replace = {0, 0, 0, 1, 7};
then the test whether "initial" has to change is based on MemberQ. All three
elements of the test are True, and only in this case the replacements apply:
In[4]:=
(MemberQ[initial, #1] & ) /@ Union[compare]
Out[4]=
{True,True,True}
The positions of the elements to be replaced in this case are
In[5]:=
replacements = Flatten[(Position[initial, #1] & ) /@
Union[compare]]
Out[5]=
{1,2,5}
So, in general, the procedure is as follows
In[6]:=
If[Union[(MemberQ[initial, #1] & ) /@
Union[compare]][[1]] == True,
initial[[replacements]] = replace[[
replacements]]];
That is, "initial" has been changed to
In[7]:=
initial
Out[7]=
{0,0,4,6,7}
Tomas Garza
Mexico City
----- Original Message -----
From: "Robert G. Wilson v" <rgwv at rgwv.com>
To: mathgroup at smc.vnet.net
Subject: [mg52011] [mg51980] List element replacement.
> Et al,
>
> HELP, I tried this once before but what I received back I could not make
> it work.
> Therefore I am following the adage that if at first you do not succeed
> then wait
> awhile, rethink, restate and resubmit.
>
> I have three lists of objects, sorted but not unioned, that is there are
> elements
> which will be repeated. Let us label them 'initial' list, 'compare' list,
> and
> 'replace' list. If all of the elements in the 'compare' list, including
> repeats,
> are in the 'initial' list, then remove those elements and put in the
> elements from
> the 'replace' list.
>
> Thank you in advance,
>
> Bob.
>
- References:
- List element replacement.
- From: "Robert G. Wilson v" <rgwv@rgwv.com>
- List element replacement.