From: Peter Pein on 11 Jan 2010 05:28 George schrieb: > Hi, > > I have a very big list of elements and I need Mathematica to give me > only those elements which occure only once... > > Could anybody advise me please how to write the code for that? > > Thks much > George > oops I forgot: Cases[Tally[theList],{x_,1}:>x,1]
From: Helen Read on 11 Jan 2010 05:27 On 1/10/2010 3:28 AM, George wrote: > Hi, > > I have a very big list of elements and I need Mathematica to give me > only those elements which occure only once... > > Could anybody advise me please how to write the code for that? Here's one way. First, make a list: list = RandomInteger[100, 75] Now select the elements that appear exactly once in the list: Select[list, Count[list, #] == 1 &] For a very large list, you might first use Union to find all elements that occur at least once in the original list. Then test each element of the union (instead of the original list with all the duplicates) and select those that occur exactly once in the original list. union=Union[list] Select[union, Count[list, #] == 1 &] BTW, it is helpful when posing questions to include a specific example (copy/paste as Input Text) so that folks replying know what you are talking about and don't have to make up their own example. -- Helen Read University of Vermont
From: Leonid Shifrin on 11 Jan 2010 05:26 Hi George, Here is a test list: In[1]:= test = RandomInteger[{1,30},30] Out[1]= {13,1,27,11,16,21,4,17,13,22,26,29,28,14,27,1,6,12,21,12,13,4,19,1,21,28,14,6,20,5} One way to do what you want: In[2]:= Select[Tally[test],Last@#==1&][[All,1]] Out[2]= {11,16,17,22,26,29,19,20,5} If your elements are integers (or reals), you can make this code quite a bit faster by compiling it: In[3] = largetest = RandomInteger[{1, 30000}, 30000]; In[4]:= fn = Compile[{{lst,_Integer,1}}, Select[Tally[lst],Last@#==1&][[All,1]],{{Tally[_],_Integer,2}}] Out[4]= CompiledFunction[{lst},Select[Tally[lst],Last[#1]==1&][[All,1]],-CompiledCode-] In[5]:= (res1 = Select[Tally[largetest],Last@#==1&][[All,1]]);//Timing Out[5]= {0.231,Null} In[6]:== (res2 = fn[largetest]); // Timing Out[6]= {0.07, Null} In[7]:= res1 == res2 Out[7]= True Hope this helps. Regards, Leonid On Sun, Jan 10, 2010 at 12:28 AM, George <gtatishvili(a)gmail.com> wrote: > Hi, > > I have a very big list of elements and I need Mathematica to give me > only those elements which occure only once... > > Could anybody advise me please how to write the code for that? > > Thks much > George > >
From: Bill Rowe on 11 Jan 2010 05:28 On 1/10/10 at 3:28 AM, gtatishvili(a)gmail.com (George) wrote: >I have a very big list of elements and I need Mathematica to give me >only those elements which occure only once... >Could anybody advise me please how to write the code for that? Here are a couple of ways. First I generate a short list likely to have both unique items and items that are duplicated In[2]:= list = RandomInteger[10, {20}] Out[2]= {6,8,10,8,9,6,6,2,2,3,4,1,4,6,1,0,10,7,8,0} Then, I extract the unique items by first sorting them then using Split to group them. In this particular case, I take advantage of knowing the items in the list are integers and use that information when specifying the pattern for Cases In[3]:= Cases[Split[Sort(a)list], {_Integer}] Out[3]= {{3}, {7}, {9}} Here is a little more general method which selects items of length 1. In[4]:= Select[Split[Sort(a)list], Length@# == 1 &] Out[4]= {{3}, {7}, {9}}
From: dh on 11 Jan 2010 05:29 Gi George, you can do this using e.g. Tally and Cases: list = {a, a, b, a, c, b, a}; Cases[Tally[list], {x_, 1} :> x] Daniel George wrote: > Hi, > > I have a very big list of elements and I need Mathematica to give me > only those elements which occure only once... > > Could anybody advise me please how to write the code for that? > > Thks much > George >
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: export differential equation solution obtained Next: NonlinearModelFit and ParameterTable |