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

• To: mathgroup at smc.vnet.net
• Subject: [mg33870] Re: [mg33863] How Do You Reduce Multiple Elements in a List?
• From: SteveC <steve at fractalus.com>
• Date: Sun, 21 Apr 2002 06:12:15 -0400 (EDT)
• References: <200204200649.CAA22326@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```* 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?