MathGroup Archive 2002

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

Search the Archive

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


Bob,

list = {1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 4, 4, 4};

Split[list]
{First[#], Length[#]} & /@ %

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

If your list is not initially in order you will want to use
Split[Sort[list]].


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



> From: Bob Harris [mailto:nitlion at mindspring.com]
To: mathgroup at smc.vnet.net
>
> Howdy,
>
> 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*.
>
> Thanks,
> Bob H
>
>



  • Prev by Date: Re: How Do You Reduce Multiple Elements in a List?
  • Next by Date: Re: How Do You Reduce Multiple Elements in a List?
  • Previous by thread: Re: How Do You Reduce Multiple Elements in a List?
  • Next by thread: Re: How Do You Reduce Multiple Elements in a List?