MathGroup Archive 2005

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

Search the Archive

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[49]:=
zaklst = {"cat", "E", "in",
    "the", "hat", "E", "okay",
    "fine", "E"}
Out[49]=
{"cat", "E", "in", "the",
   "hat", "E", "okay", "fine",
   "E"}

In[60]:=
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[60]=
{{"cat"}, {"in", "the",
    "hat"}, {"okay", "fine"}}


Here is a "functional" one:

In[79]:=
(Rest[Most[Take[Join[{"E"},
        zaklst], #1]]] & ) /@
   Partition[Flatten[
     Position[Join[{"E"},
       zaklst], "E"]], 2, 1]
Out[79]=
{{"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