Re: How Do You Reduce Multiple Elements in a List?
- To: mathgroup at smc.vnet.net
- Subject: [mg33871] Re: [mg33863] How Do You Reduce Multiple Elements in a List?
- From: BobHanlon at aol.com
- Date: Sun, 21 Apr 2002 06:12:17 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 4/20/02 4:25:28 AM, nitlion at mindspring.com writes:
>This seems like such a simple thing, yet I've search through the big book
>trying to figure it out. Perhaps someone can help me...
>
>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}}
>
>Is there a built-in function to do this? Seems like a histogramming
>operation, but I get no useful matches from ?*Hist*.
>
lst = Table[Random[Integer,{1,4}],{10}];
Look in the index under run length encoding. You will be directed to the
function Split
{First[#],Length[#]}&/@Split[Sort[lst]]
or
{#,Count[lst, #]}& /@ Union[lst]
or
Needs["Statistics`DataManipulation`"];
Reverse /@ Frequencies[lst]
Bob Hanlon
Chantilly, VA USA