Re: Better way to write this
- To: mathgroup at smc.vnet.net
- Subject: [mg85076] Re: Better way to write this
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sun, 27 Jan 2008 05:45:38 -0500 (EST)
- References: <fnet45$id5$1@smc.vnet.net>
On Jan 26, 1:05 am, Louis Theran <the... at gmail.com> wrote: > I have the following function that takes as its input a number of > lists and returns the elements in each of them that appear exactly > once: > > SpecialElements[lists:_List..]:= > Module[{l,n,Sower,Reaper}, > l = {lists}; n = Length[l]; > Sower[l_,{idx_}]:= Sow[idx,{#}] & /@ l; > Reaper[val_,{place_}] := Sow[val,place]; > Reaper[val_,x_]:= Null; > Reap[Reap[MapIndexed[Sower, l],_,Reaper],Range[n]][[2]]] > > This works pretty well, but since I am using it on rather large > inputs, I was wondering if there is a faster way to implement it than > Reap/Sow, which is sort of counting in unary. > > ^L I haven't tried these, but any one of them should return a list of the elements that appear exactly once in its argument. Mapping across your list of lists would then give what you want. f1[v_List] := Flatten@Select[Split@Sort@v,Length@#==1&] f2[v_List] := Select[Union@v,Count[v,#]==1&] f3[v_List] := Pick[Sequence@@Transpose@Tally@v,1]