Re: List element replacement.

*To*: mathgroup at smc.vnet.net*Subject*: [mg52022] Re: [mg51980] List element replacement.*From*: DrBob <drbob at bigfoot.com>*Date*: Sun, 7 Nov 2004 01:04:06 -0500 (EST)*References*: <200411060707.CAA25986@smc.vnet.net>*Reply-to*: drbob at bigfoot.com*Sender*: owner-wri-mathgroup at wolfram.com

Does this do the trick? (My testing wasn't extensive.) ClearAll@removeAndReplace removeAndReplace::usage = " removeAndReplace[a,b,c] checks whether List a is contained in List b (counting multiplicities). If no, b is returned. If \ yes, the return value is b with the elements of a removed and the elements of \ c added (counting multiplicities and sorted)." removeAndReplace[a_List, b_List, c_List] := Block[{aFreq, bFreq, f, pos}, Which[ Length@a > Length@b, b, Length@(aFreq = Frequencies@a) > Length@(bFreq = Frequencies@b), b, f[{n_, x_}] := ((pos = Position[bFreq[[All, 2]], x]) === {} || (n > Extract[bFreq, {pos[[1, 1]], 1}])) && Throw[False]; Catch[Scan[f, aFreq]; Throw@True], f[lst_, {n_, x_}] := DeleteCases[lst, x, {1}, n]; Sort@Fold[f, Join[b, c], aFreq], True, b] ] This could be useful without the Sort, so I'd probably omit it and enter Sort@removeAndReplace[a,b,c], if I needed a sorted result. In that case, when it removes elements, it removes them starting at the front of Join[b,c]. Bobby On Sat, 6 Nov 2004 02:07:45 -0500 (EST), Robert G. Wilson v <rgwv at rgwv.com> wrote: > 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. > > > > -- DrBob at bigfoot.com www.eclecticdreams.net

**References**:**List element replacement.***From:*"Robert G. Wilson v" <rgwv@rgwv.com>