Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

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[1]:=
a = {cat, E, in, the, hat, E, okay, fine, E};

A step-by-step solution easy to follow:

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

This would be the "natural" answer:

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

Still, you may get your proposed answer as

In[7]:=
{#} & /@ Take[a, #] & ) /@ f
Out[7]=
{{{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[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: 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