Re: Intersection @ Complement
- To: mathgroup at smc.vnet.net
- Subject: [mg58152] Re: Intersection @ Complement
- From: Zak Seidov <zakseidov at yahoo.com>
- Date: Mon, 20 Jun 2005 05:21:27 -0400 (EDT)
- References: <d938l7$7fh$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
David Bailey Garry Helzer Murray Eisenberg thanks a lot for your replies. I found in Mathematica 5.1 Help exactly what i need: 1) MultiIntersection generalizes Intersection. If an element occurs m times in one list and n times in another, MultiIntersection will list it Min[m, n] times. MultiIntersection[l1_List,l2_List]:=Module[{nl,f},f[x_]:={First[#],Length[#]}&/@Split[Sort[x]];nl=Sort[Join[Flatten[Map[f,{l1,l2}],1]]];nl=Split[nl,#[[1]]===#2[[1]]&];Flatten[Cases[nl,{{x_,m_},{x_,n_}}\[RuleDelayed]Table[x,{m}]],1]] MultiIntersection[{b,b,a,b,a},{a,a,a,b}] {a,a,b} (* !! *) 2) This defines a version of Complement that does not sort. An element in the first list is dropped if it occurs in one of the other lists. The remaining elements are listed with their original multiplicities. UnsortedComplement[x_List,y__List]:=Replace[x,Dispatch[(#\[RuleDelayed]Sequence[])&/@Union[y]],1] UnsortedComplement[{a,b,b,b,c,a,e,d,e},{e,b,f},{b,d}] {a,c,a} (* !! *) Complement[{a,b,b,b,c,a,e,d,e},{e,b,f},{b,d}] {a,c} zak