Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Occurrences in Mathematica

67 views
Skip to first unread message

George

unread,
Jan 10, 2010, 3:28:40 AM1/10/10
to
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

Bob Hanlon

unread,
Jan 11, 2010, 5:26:51 AM1/11/10
to

data = RandomInteger[{0, 9}, 20]

{5,5,9,2,6,5,3,9,1,6,1,0,9,6,4,5,8,6,7,5}

Select[Union[data], Count[data, #] == 1 &]

{0,2,3,4,7,8}

Cases[Union[data], _?(Count[data, #] == 1 &)]

{0,2,3,4,7,8}

DeleteCases[Union[data], _?(Count[data, #] > 1 &)]

{0,2,3,4,7,8}

Cases[{#, Count[data, #]} & /@ Union[data], {x_, 1} -> x]

{0,2,3,4,7,8}


Bob Hanlon

---- George <gtati...@gmail.com> wrote:

=============

DrMajorBob

unread,
Jan 11, 2010, 5:27:13 AM1/11/10
to
list = RandomInteger[{0, 5}, 7]

{0, 0, 0, 4, 5, 2, 3}

Cases[Tally[list], {x_, 1} :> x]

{4, 5, 2, 3}

Bobby

On Sun, 10 Jan 2010 02:28:47 -0600, George <gtati...@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
>


--
DrMaj...@yahoo.com

Murray Eisenberg

unread,
Jan 11, 2010, 5:27:57 AM1/11/10
to
Here's one way, via an example:

lis = RandomChoice[Characters["abcdefghijklmnopqrstuvwxyz"], 50];

Select[Union[lis], Count[lis, #] == 2 &]

Your results will vary according to what RandomChoice gives you. But the
code does exactly what you asked: it selects (Select) from the unique
elements of the list (Union[lis]) those elements for which the Count of
their appearances in the list is 2.

I make no claims for the efficiency of the code. That may depend upon
the length and perhaps even the elements of your list, among other things.

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?
>
> Thks much
> George
>

--
Murray Eisenberg mur...@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

Peter Pein

unread,
Jan 11, 2010, 5:28:08 AM1/11/10
to
George schrieb:

Hi George,

calculating some data:

SeedRandom[5];
theList=Insert[RandomChoice[#/Tr[#]&[1/#^4]->#,{10^6}]&[Range[100]],{a,b,a,c},123456];

to preserve the order of occurrance, I would use:

Reap[
NestWhile[
(If[FreeQ[##1],
Sow[#2];#1,
DeleteCases[##1]]&)@@{Rest[#1],First[#1]}&,
theList,
#1=!={}&]
][[2,1]]

which gives: {26,29,35,{a,b,a,c},40,60,28,34,33,31,32}

If you want sorted results, use the simple (and fast)

Cases[Split[Sort[theList]],{x_}:>x]

---> {26,28,29,31,32,33,34,35,40,60,{a,b,a,c}}


HTH,
Peter

Peter Pein

unread,
Jan 11, 2010, 5:28:20 AM1/11/10
to
George schrieb:

oops I forgot: Cases[Tally[theList],{x_,1}:>x,1]


Leonid Shifrin

unread,
Jan 11, 2010, 5:26:40 AM1/11/10
to
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

Helen Read

unread,
Jan 11, 2010, 5:27:02 AM1/11/10
to
On 1/10/2010 3:28 AM, George wrote:

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

Bill Rowe

unread,
Jan 11, 2010, 5:28:30 AM1/11/10
to
On 1/10/10 at 3:28 AM, gtati...@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@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@list], Length@# == 1 &]

Out[4]= {{3}, {7}, {9}}


dh

unread,
Jan 11, 2010, 5:29:37 AM1/11/10
to

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

brien colwell

unread,
Jan 11, 2010, 5:29:47 AM1/11/10
to
I'd start with something simple like

Cases[Tally[list], {a_, 1} :> a]

Albert Retey

unread,
Jan 11, 2010, 5:30:31 AM1/11/10
to
If you use version 6 or 7, maybe like this?

list = RandomInteger[1000, 1000];

Cases[Tally[list], {x_, 1} :> x]

hth,

albert

Harvey P. Dale

unread,
Jan 11, 2010, 5:31:15 AM1/11/10
to
Try this:

Select[BinLists[{yourlist}],Length[#]==1&]

Best,

Harvey

-----Original Message-----
From: George [mailto:gtati...@gmail.com]
Sent: Sunday, January 10, 2010 3:29 AM
Subject: Occurrences in Mathematica

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


Ray Koopman

unread,
Jan 11, 2010, 6:52:39 PM1/11/10
to

It takes longer for Cases to process the Tally result
than it takes Tally to generate it. Pick is faster.

In[1]:= Timing@Length[a = RandomInteger[999999,1*^6]]
Out[1]= {0.05,1000000}

In[2]:= Timing@Length[b = Tally@a]
Out[2]= {1.59,631912}

In[3]:= Timing@Length[c = Cases[b,{x_,1}:>x]]
Out[3]= {1.73,367228}

In[4]:= Timing@Length[d = Pick[Sequence@@Transpose@b,1]]
Out[4]= {0.5,367228}

In[5]:= c === d
Out[5]= True

0 new messages