Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*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 1999

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

Search the Archive

Re: Finding lengths of identical sequences in lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20274] Re: [mg20240] Finding lengths of identical sequences in lists
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sun, 10 Oct 1999 01:27:30 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Joel,

Check out the Split command. Map Length onto the result.

Length /@ Split[{3, 2, 2, 3, 3, 3, 1}]
{1, 2, 3, 1}

Length /@ Split[{{1, 3}, {2, 5}, {2, 5}, {3, 2}, {3, 2}, {3, 2}, {1, 3}}]
{1, 2, 3, 1}

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

>I am looking for a more elegant solution to the following. I have a
>list (a list of positions at consecutive times).  I wish to create a
>new list of the length of each sequence of consecutive identical
>members (including those of length 1). 
>
>For example, for the list {3,2,2,3,3,3,1} I would create the list
>{1,2,3,1} because the first 3 appears once, followed by two 2's,
>etc. The lists I was working with contained a list of numbers
>(e.g. {{x1,y1},{x2,y2},...} rather than simple numbers. I accomplished
>this using a while loop, but I suspect there are better ways to do
>this using pattern matching or other approaches.
>
>Thanks much.
>
>
>------------------------------------------------------------------------------
>Joel W. Cannon                   |   (724)223-6146         
>Dept. of Physics                 |  
>Washington and Jefferson College |      
>Washington, PA 15301             |      
>                                     
>                    
>
>
>



  • Prev by Date: Re: Replacing Part of a Matrix
  • Next by Date: Tough Integral
  • Previous by thread: Re: Finding lengths of identical sequences in lists
  • Next by thread: RE: Why Doesn't N[Pi,i] Give i Digits For Small i, Math ematica 4,on NT