       Re: Re: List Partition

• To: mathgroup at smc.vnet.net
• Subject: [mg53940] Re: [mg53899] Re: [mg53895] List Partition
• From: Tomas Garza <tgarza01 at prodigy.net.mx>
• Date: Fri, 4 Feb 2005 04:12:24 -0500 (EST)
• References: <200502021125.GAA29007@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Another possibility along the "functional" approach:

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

A step-by-step solution easy to follow:

In:=
b = Flatten[Position[a, E]];
c = Prepend[Most[b] + 1, 1];
d = MapThread[List, {c, b - 1}];
f = (If[#[] == #[], {#[]}, #] & ) /@ d;

This would be the "natural" answer:

In:=
(Take[a, #] & ) /@ f
Out=
{{cat}, {in, the, hat}, {okay, fine}}

In:=
{#} & /@ Take[a, #] & ) /@ f
Out=
{{{cat}}, {{in}, {the}, {hat}}, {{okay}, {fine}}}

Tomas Garza
Mexico City
----- Original Message -----
From: "János" <janos.lobb at yale.edu>
To: mathgroup at smc.vnet.net
Subject: [mg53940] [mg53899] Re: [mg53895] List Partition

>
> 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: Re: Re: Latex + Mathematica EPS file problems
• Next by Date: Re: random matrix from row and column sums
• Previous by thread: Re: List Partition
• Next by thread: Re: List Partition