Re: List element replacement.

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

Here's a more readable routine (I think): 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)." removeAndReplace[a_List, b_List, c_List] := Block[{aFreq, bFreq, aa, bb, f}, If[ Or[ Length@a > Length@b, {aFreq, bFreq} = Frequencies /@ {a, b}; Length@aFreq > Length@bFreq, {aa, bb} = {aFreq, bFreq}[[All, All, 2]]; Union[aa, bb] =!= Union[bb], f[{n_, x_}] := n > Extract[bFreq, {Position[bb, x][[1, 1]], 1}] && Throw@True; Catch[Scan[f, aFreq]; False] ], b, Clear@f; f[lst_, {n_, x_}] := DeleteCases[lst, x, {1}, n]; Fold[f, Join[b, c], aFreq]] ] I removed Sort and added a check with Union, among other things. Bobby On Sat, 06 Nov 2004 16:12:24 -0600, DrBob <drbob at bigfoot.com> wrote: > 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>