Re: Eliminate duplicates using Union [] in a
- To: mathgroup at smc.vnet.net
- Subject: [mg48184] Re: Eliminate duplicates using Union [] in a
- From: Torben Winther Graversen <immtwg at nospam.student.dtu.dk>
- Date: Mon, 17 May 2004 03:21:52 -0400 (EDT)
- Organization: UNI-C
- References: <c7nn6h$dl5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Mark Coleman wrote: > I know that if one applies the Union command to a list of values, it > will return the list with duplicates removed. Can I use the Union > command to eliminate duplicate when I have a list of data "records"? Hi Mark, Yes, you can. Use the SameTest option of Union to specify a function which returns true if two elements are considered equal and false otherwise. In your case, you say you need to compare a pair of fields from each record to each other. You might try something like, if say the two fields have indexes 2 and 3: In[15]:= dataset = {{1, 2, 3}, {4, 5, 6}, {3, 2, 3}}; (* First and third are "duplicates" *) In[16]:= MySameTest[a_, b_] := {a[[2]], a[[3]]} === {b[[2]], b[[3]]} In[17]:= Union[dataset, SameTest -> MySameTest] Out[17]= {{1, 2, 3}, {4, 5, 6}} In[18]:= (* or, using a pure function: *) In[19]:= Union[dataset, SameTest -> ({#1[[2]], #1[[3]]} === {#2[[2]], #2[[3]]} &)] Out[19]= {{1, 2, 3}, {4, 5, 6}} Hope this helps, - Torben