Re: List complement operator
- To: mathgroup at smc.vnet.net
- Subject: [mg85057] Re: List complement operator
- From: Roland Franzius <roland.franzius at uos.de>
- Date: Sat, 26 Jan 2008 05:00:16 -0500 (EST)
- Organization: Universitaet Hannover
- References: <fn1ndi$97t$1@smc.vnet.net>
zac schrieb: > 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? sublists[a_List, b_List] := (Part[#, Complement[ Range[Length[a]], Union@Flatten@((Position[a, #] &) /@ b)] ] &)[Sort[a]] -- Roland Franzius