MathGroup Archive 2003

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

Search the Archive

Re: split a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40594] Re: split a list
  • From: Bill Rowe <listuser at earthlink.net>
  • Date: Thu, 10 Apr 2003 03:44:27 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 4/9/03 at 8:27 PM, majort at cox-internet.com (Dr Bob) wrote:

>This is a beautiful application for Sow and Reap.
>
><< Experimental` testList[n_Integer] := Array[Random[] &, n]
>trial[n_Integer] := {testList@n, Random[]}
>
>breakList[test_List, break_] := Reap[If[# < break, Sow[#, 1], Sow[#,
>2]] & /@ test]
>
>trial@30 breakList @@ %
>
>That version may not be ideal, because if all elements are on the same
>side of the break point, you get only one list back -- not two, with
>one of them empty.  If that's a concern (but with thousands of
>elements it may never come up), this function fixes it:
>
>breakList2[test_List, break_] := Rest /@ Reap[Sow[0, #] & /@ {1,2};
>If[# < break, Sow[#, 1], Sow[#, 2]] & /@ test]
>
>Here's another solution to that problem:
>
>breakList3[test_List, break_] := Flatten /@ Reap[If[# < break, Sow[#,
>1], Sow[#, 2]] & /@ test, {1, 2}]

Interesting but the simple function

sp[x_List,m_]:={Select[x,#<m&], Select[x, #>m&]}

seems to be both more efficient and clearer as to intent

data = Table[Random[],{1000000}];

Timing[sp[data,.3];]
{1.41 Second,Null}

Timing[breakList2[data,.3];]
{1.96 Second,Null}

Timing[breakList3[data,.3];]
{1.71 Second,Null}


  • Prev by Date: Raster process
  • Next by Date: Re: numerical integration
  • Previous by thread: RE: split a list
  • Next by thread: RE: split a list