MathGroup Archive 2002

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

Search the Archive

Re: How Do You Reduce Multiple Elements in a List?


* Bob Harris (nitlion at mindspring.com) wrote:
> Given a list of integers that generally contains the same number many times,
> I'd like to reduce it to a list that just gives me the number/count pairs.
> For example, I'd like to convert the list
>    {1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 4, 4, 4}
> into
>    {{1,5}, {2,3}, (4,6}}

l = {1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 4, 4, 4};
Map[{#, Count[l, #]} &, Union[l]]

{{1, 5}, {2, 3}, {4, 6}}

or see Statistics`DataManipulation`

<< Statistics`DataManipulation`
Frequencies[l]


{{5, 1}, {3, 2}, {6, 4}}
...which returns it the other way around...

have fun,

SteveC steve at fractalus.com fractalus.com/steve


  • Prev by Date: "Message text not found" substring in the warning messages
  • Next by Date: Re: How Do You Reduce Multiple Elements in a List?
  • Previous by thread: How Do You Reduce Multiple Elements in a List?
  • Next by thread: Re: How Do You Reduce Multiple Elements in a List?