MathGroup Archive 2008

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

Search the Archive

Re: Better way to write this

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85084] Re: [mg85058] Better way to write this
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sun, 27 Jan 2008 05:49:46 -0500 (EST)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200801261000.FAA21110@smc.vnet.net>
  • Reply-to: murray at math.umass.edu

Try the following:

   listUnique[lis_List] := Pick[lis, (Count[lis,#]==1)& /@ lis]

For example:

   lis1 = {a, b, b, c};
   lis2 = {x, x, y, z, x, w};
   listUnique /@ {lis1, lis2}
{{a,c},{y,z,w}}

I haven't checked timings, but in general it's a good idea to exploit 
kernel functions that work on whole lists at once, e.g., Pick and Count.


Louis Theran 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
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Problems with Dynamic Updating and Java Runtime
  • Next by Date: Re: Find Upper Neighbor in a list
  • Previous by thread: Better way to write this
  • Next by thread: Re: Better way to write this