Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

RE: Any better way for finding frequencies of list entries?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg26947] RE: [mg26923] Any better way for finding frequencies of list entries?
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sat, 27 Jan 2001 20:00:13 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Elegance or speed? In any case, converting to strings and characters is
excessive and unnecessary.

Here is one solution. RealDigits extracts a list containing the digits or a
real number, plus how many are to the left of the decimal point (which we
throw away). Split divides a list into sublists for which all adjacent
elements are the same.

number = N[Pi, 200];

{First[#], Length[#]} & /@ Split[Sort[First[RealDigits[number]]]]
{{0, 20}, {1, 19}, {2, 25}, {3, 20}, {4, 22}, {5, 20}, {6, 15}, {7, 12}, {8,
    25}, {9, 22}}

That was the fastest solution I found. Another possible solution, only half
as fast, is:

cntarray = Table[0, {10}];
(++cntarray[[#1 + 1]] & ) /@ First[RealDigits[number]];
Transpose[{Range[0, 9], cntarray}]

I obtain the same Timing using Scan

cntarray = Table[0, {10}];
Scan[++cntarray[[#1 + 1]] &, First[RealDigits[number]]];
Transpose[{Range[0, 9], cntarray}]

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


> From: Domi [mailto:doud25 at caramail.com]
To: mathgroup at smc.vnet.net
>
> Hello Mathematica experts out there,
>
> I want to determine the frequencies of the entries in a list of integers
>
> For example, the following piece of code seems to work with Pi:
> pi = Join
>     [ {3}, ToExpression[Characters[StringDrop[ToString[N[Pi, 57]], {1,
> 2}]]]]
> Table[{i, Count[pi, i]}, {i, 0, 9}]
>
>     {{0, 3}, {1, 5}, {2, 6}, {3, 9}, {4, 4}, {5, 6}, {6, 4}, {7, 5},
>     {8, 6}, {9,   9}}
>
> Question: Is there a more elegant way to do that?
>
> Regards from Domi.
>
>



  • Prev by Date: Re: G4 power Mac and OS X
  • Next by Date: Re: Any better way for finding frequencies of list entries?
  • Previous by thread: Re: Any better way for finding frequencies of list entries?
  • Next by thread: Re: Any better way for finding frequencies of list entries?