Re: Re: split a list
- To: mathgroup at smc.vnet.net
- Subject: [mg40592] Re: [mg40537] Re: split a list
- From: Dr Bob <majort at cox-internet.com>
- Date: Thu, 10 Apr 2003 03:44:05 -0400 (EDT)
- References: <b70boj$883$1@smc.vnet.net> <200304100026.UAA21553@smc.vnet.net>
- Reply-to: majort at cox-internet.com
- Sender: owner-wri-mathgroup at wolfram.com
Experimental package and test data: << Experimental` testList[n_Integer] := Array[Random[] &, n] trial[n_Integer] := {testList@n, Random[]} Three solutions: treat[test_List, break_] := Flatten /@ Reap[If[# â?¤ break, Sow[#, 1], Sow[#,2]] & /@ test, {1, 2}] brambilla[r_, m_] := Module[{tmp, u, v, i}, u = v = {}; For[i = 1, i â?¤ Length[r], i++, tmp = r[[i]]; If[tmp > m, AppendTo[u, tmp], AppendTo[v, tmp]];]; {v, u}] kuska[r_, m_] := {Select[r, # â?¤ m &], Select[r, # > m &]} Accuracy (one of many): test = trial@5; brambilla @@ test == kuska @@ test == treat @@ test True (Small n is a better accuracy test than large, for this one!) Timing: test = trial@50000; Timing[brambilla @@ test;] Timing[treat @@ test;] Timing[kuska @@ test;] {210.015*Second, Null} {0.39100000000001955*Second, Null} {0.4219999999999686*Second, Null} test = trial@500000; Timing[treat @@ test;] Timing[kuska @@ test;] {3.906000000000006*Second, Null} {4.187999999999988*Second, Null} test = trial@1500000; Timing[kuska @@ test;] Timing[treat @@ test;] Timing[treat @@ test;] Timing[kuska @@ test;] {12.718999999999994*Second, Null} {12.625*Second, Null} {12.375*Second, Null} {13.078000000000031*Second, Null} 'treat' doesn't win every time, and the difference is fairly small. I think a division according to several breakpoints (not just one m) would favor my method more clearly. Bobby On Wed, 9 Apr 2003 20:26:16 -0400 (EDT), Jens-Peer Kuska <kuska at informatik.uni-leipzig.de> wrote: > Hi, > > lst = Table[Random[], {10000}]; > > (* your version *) > > SplitAt[r_, m_] := Module[{tmp, u, v, i}, > u = v = {}; > For[i = 1, i <= Length[r], i++, tmp = r[[i]]; > If[tmp > m, AppendTo[u, tmp], AppendTo[v, tmp]];]; > {v, u} > ] > > (* a faster one *) > > SplitAt1[r_, m_] := {Select[r, # < m &], Select[r, # >= m &]} > > > Timing[SplitAt[lst, 0.5];] > > {24.22 Second, Null} > > and > > Timing[SplitAt1[lst, 0.5];] > > {0.6 Second, Null} > > Regards > Jens > > Roberto Brambilla wrote: >> >> Hi, >> >> I have a list (very long, thousands, and unsorted) of numbers >> r={n1,n2...} >> and for a given a number m I want to split it in the two sublists >> (unsorted, same order) u={elements<m], v={elements>m}. >> Now I use this *old-style* method: >> >> u = v = {}; >> For[i = 1, i <= Length[r], i++, >> tmp = r[[i]]; >> If[tmp > m , AppendTo[u, tmp], AppendTo[v, tmp]]; >> ] >> >> Any suggestion for a more efficient (and elegant) method? >> Also oneliners are well accepted. >> >> Many thanks, Roberto >> >> Roberto Brambilla >> CESI >> Via Rubattino 54 >> 20134 Milano >> tel +39.02.2125.5875 >> fax +39.02.2125.5492 >> rlbrambilla at cesi.it > > -- majort at cox-internet.com Bobby R. Treat
- References:
- Re: split a list
- From: Jens-Peer Kuska <kuska@informatik.uni-leipzig.de>
- Re: split a list