Re: Re: Efficient way to merge lists--Programming help
- To: mathgroup at smc.vnet.net
- Subject: [mg12801] Re: Re: Efficient way to merge lists--Programming help
- From: "Allan Hayes" <hay at haystack.demon.cc.uk>
- Date: Fri, 12 Jun 1998 04:05:33 -0400
- References: <6ll172$elg$1@dragonfly.wolfram.com>
- Sender: owner-wri-mathgroup at wolfram.com
Carl Woll wrote in message <6ll172$elg$1 at dragonfly.wolfram.com>... >Hi Daniel, > >As you said, we can now compare timings of different solutions. I gave a >slightly different solution than yours, and it had an error. Also, >after comparing my fixed solution with yours, I realized how to speed >it up slightly. My tests show that the following solution is slightly >faster than yours. > >repair[rough_, fine_] := Module[{f,g,result}, > f[{a_,b_,c_}] := g[a,b,_] = {a,b,c}; > f /@ fine; > result = Apply[g, rough, {2}]; > g = List; > result >] > ...................[cut] ........... Carl, It looks as if you need {1}instead of {2} in line 4. g still has to do a lot of searching. The following variant is about twice as quick on my tests (its important to limit the search by Position) repair2[rough_, fine_] := Module[{f, r=rough}, f[s:{a___,_}] := r[[First[Position[r,{a,_},{1},1,Heads->False]]]] = s; f /@ fine; r ] Other variants, like using positions in both lists don't seem to be of much benefit. Allan ------------------------------------------------------------- Allan Hayes Training and Consulting Leicester UK http://www.haystack.demon.co.uk hay at haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44(0)116 271 8642