       Re: List Partition

• To: mathgroup at smc.vnet.net
• Subject: [mg53941] Re: [mg53895] List Partition
• From: John Kiehl <john.kiehl at soundtrackny.com>
• Date: Fri, 4 Feb 2005 04:12:26 -0500 (EST)
• Reply-to: John Kiehl <john.kiehl at soundtrackny.com>
• Sender: owner-wri-mathgroup at wolfram.com

```
On Wednesday, February 2, 2005 6:25 AM, zak <chocolatez at gmail.com> wrote:
>what could i do to partition the list:
>a={cat,E,in,the,hat,E,okay,fine,E}
>into
>a={{cat},{{in},{the},{hat}},{{okay},{fine}}}
>
>ie:  every word in a sublist , and E determine the end of a
>sentence
>(a bigger list).
>zak
>
>

In:=  a={cat,E,in,the,hat,E,okay,fine,E}
Out=    {cat,E,in,the,hat,E,okay,fine,E}

This solution is an automation of this basic idea.

In:=  Part[a,{1}]
Out=  {cat}

In:=  Part[a,{3,4,5}]
Out=  {in,the,hat}

In:=  Part[a,{7,8}]
Out=  {okay,fine}

The "Position" of E in the original list will be used to calculate the start and end of each sub-list.

(*-------------------------------------------------*)
(* so, first put brackets around each element as requested *)
In=    aa=Map[List,a]
Out=  {{cat},{E},{in},{the},{hat},{E},{okay},{fine},{E}}

(* in general, we want to find positions of {E} in list *)
In=    pos = Position[aa,{E}]//Flatten
Out=   {2,6,9}

(* one less than pos will be end of each sublist *)
In:=   last=pos-1
Out:=   {1,5,8}

(* one more than pos will be start of each sublist,
also drop last value and prepend a 1 *)
In:=   first=Drop[Flatten[{1,pos}],-1]
Out=   {1,3,7}

(* pair these up *)
In:=   t=Transpose[{first,last}]
Out=   {{1,1},{3,5},{7,8}}

(* map Part[...] over our list t *)
In:=   Part[aa,Range[Sequence @@ #]]& /@ t
Out=   {{{cat}},{{in},{the},{hat}},{{okay},{fine}}}

```

• Prev by Date: Re: Problem with a sum
• Next by Date: Re: Matrix equations
• Previous by thread: Re: List Partition
• Next by thread: Re: List Partition