Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: Partition a list into sublists of unequal size

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83963] Re: [mg83919] Partition a list into sublists of unequal size
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Wed, 5 Dec 2007 07:20:14 -0500 (EST)
  • References: <9795325.1196789700710.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

How's this?

list = {1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0};
subSeqs = Split[list, #1 == 0 &]

{{1}, {0, 0, 1}, {0, 0, 1}, {1}, {0, 1}, {0, 0, 0, 0, 1}, {0}}

subSeqs /. {0 ..} :> Sequence[]

{{1}, {0, 0, 1}, {0, 0, 1}, {1}, {0, 1}, {0, 0, 0, 0, 1}}

For a list of just zeroes, that goes like

list = {0, 0, 0};
subSeqs = Split[list, #1 == 0 &]

{{0, 0, 0}}

subSeqs /. {0 ..} :> Sequence[]

{}

And if it's all ones:

list = {1, 1, 1};
subSeqs = Split[list, #1 == 0 &]

{{1}, {1}, {1}}

subSeqs /. {0 ..} :> Sequence[]

{{1}, {1}, {1}}

Bobby

On Tue, 04 Dec 2007 03:29:39 -0600, antf <anthonyfox at gmail.com> wrote:

> Hello,
>
> I have a list that looks like the following:
>
> list={1,0,0,1,0,0,1,1,0,1,0,0,0,0,1,0}
>
> I then find the positions of the 1s in the list:
>
> positions = Flatten@Position[list,1]
>
> {1,4,7,8,10,15}
>
> I then want to partition a third list into sublists so that the first
> sublist is from index 1;;1, the second sublist is from index 2;;4, the
> third sublist is from index 5;;7, the fourth sublist is just element
> 8, etc.
>
> Currently, I use Partition on the positions sublist with overlap as
> in:
>
> Partition[Join[{1},positions],2,1]
>
> {{1,1},{1,4},{4,7},{7,8},{8,10},{10,15}}
>
> I then use this list to partition the target list by Mapping Take to
> each element of this list and adjusting the first index appropriately.
>
> I am sure there must be a simpler way to do this.  Can anyone tell me
> how to do this operation?
>
> Thanks,
> Anthony
>
>



-- 

DrMajorBob at bigfoot.com


  • Prev by Date: Re: slot argument weirdness
  • Next by Date: Re: Partition a list into sublists of unequal size
  • Previous by thread: Re: Partition a list into sublists of unequal size
  • Next by thread: Re: Partition a list into sublists of unequal size