MathGroup Archive 2006

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

Search the Archive

Re: Partition


Off[General::spell1];

f1[list_List,n_Integer]:=Flatten[{First[#],Rest[#]}&/@Partition[list,n],1]

f2[list_List,n_Integer]:=
      Partition[list,n]/.a_?(Length[#]==n&):>Sequence[First[a],Rest[a]];

f3[list_List,n_Integer]:=Module[{k},Sequence@@@Table[{list[[k]],
      Take[list,k+{1,n-1}]},{k,1,Length[list],n}]];

list={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o};

f1[list,5]

{a,{b,c,d,e},f,{g,h,i,j},k,{l,m,n,o}}

%==f2[list,5]==f3[list,5]

True

list=Table[Random[],{200000}];

Timing[f1[list,5];]

{0.731719 Second,Null}

Timing[f2[list,5];]

{0.889727 Second,Null}

Timing[f3[list,5];]

{1.14295 Second,Null}


Bob Hanlon

---- KFUPM <hussain.alqahtani at gmail.com> wrote: 
> Dear All
> 
> I have a very large list of numbers that look like this: ( although the
> size is different)
> 
> list={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o}
> 
> And i need to rewrite the list into
> 
> list2={a,{b,c,d,e},f,{g,h,i,j,},k,{l,m,n,o}}
> 
> Since my list is too big, i need Mathematica to do it for me regardless
> of the site of the list.
> 
> I would appreciate it if you can help me in this regard,
> 
> Thanks
> 
> KFUPM
> 


  • Prev by Date: Re: Area of ellipse between major axis and ray through focus, given angle
  • Next by Date: RE: Nonlinear Regression
  • Previous by thread: Re: Partition
  • Next by thread: Mathematica "Interpolation" function