       Re: List Partition

• To: mathgroup at smc.vnet.net
• Subject: [mg53899] Re: [mg53895] List Partition
• From: János <janos.lobb at yale.edu>
• Date: Wed, 2 Feb 2005 18:10:40 -0500 (EST)
• References: <200502021125.GAA29007@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On Feb 2, 2005, at 6:25 AM, zak 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
>

Here is a "procedural" solution.

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

In:=
zakprt = First[Last[
Reap[While[zaklst =!= {},
current = Take[zaklst,
{1, First[Flatten[
Position[zaklst,
"E"]]] - 1}];
zaklst = Take[zaklst,
{First[Flatten[
Position[zaklst,
"E"]]] + 1,
Length[zaklst]}];
Sow[current]; ]]]]
Out=
{{"cat"}, {"in", "the",
"hat"}, {"okay", "fine"}}

Here is a "functional" one:

In:=
(Rest[Most[Take[Join[{"E"},
zaklst], #1]]] & ) /@
Partition[Flatten[
Position[Join[{"E"},
zaklst], "E"]], 2, 1]
Out=
{{"cat"}, {"in", "the",
"hat"}, {"okay", "fine"}}

I learned all these techniques from the members of this list.
János

----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)

```

• Prev by Date: Introduction to Programming with Mathematica, 3rd ed.
• Next by Date: Re: Wald-Wolfowitz runs test
• Previous by thread: List Partition
• Next by thread: Re: List Partition