MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List complement operator

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85001] Re: List complement operator
  • From: dh <dh at metrohm.ch>
  • Date: Tue, 22 Jan 2008 01:58:09 -0500 (EST)
  • References: <fn1ndi$97t$1@smc.vnet.net>


Hi Istvan,

here is a possible solution. We sort the lists, split them into sublists 

  of identical elements and replace each sublist by a tupel {x1,x2} 

where x1= elment, x2= count of elements. Finally we replace each tupel 

that appears twice by  a singel tupel with the x2 subtracted. And then 

we delete tupels with element count =0.



setSubtr[d1_List,d2_List]:=Module[{t},

t= Join@@ (Split@Sort[#]& /@ {d1,d2})  ;

t=t/.x1:{x_ ..}:>{x,Length[x1]};

t=t//.  {x1___,{x2_,x3_},x4___,{x2_,x5_},x6___}:>{x1,{x2,x3-x5},x4,x6}  ;

Select[t,#[[2]]!=0&]

]

hope this helps, Daniel



zac wrote:

> Dear group,

> 

> I'm loooking for an elegant and efficient way to subtract a list (not

> a set) from an other one.

> 

> a = {1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 2, 6, 7, 7};

> b = {1, 2, 2, 3, 4, 7, 8};

> 

> Complement[a,b] returns {5, 6}, as expected. However I would like to

> treat each entry as a discrete element, therefore my target result is:

> {1, 1, 3, 5, 5, 5, 6, 7}. Union can not be used, although Sort  is

> allowed (ie. whether the result is sorted or not is of no concern).

> Any idea?

> 

> Istvan Zachar

> 




  • Prev by Date: Re: Re: Fonts in Mathematica 6.0
  • Next by Date: 2D Interpolation
  • Previous by thread: Re: List complement operator
  • Next by thread: Re: List complement operator