Re: Efficient way to merge lists--Programming help

*To*: mathgroup at smc.vnet.net*Subject*: [mg12721] Re: [mg12708] Efficient way to merge lists--Programming help*From*: Reinhold Kainhofer <reinhold.kainhofer at kfunigraz.ac.at>*Date*: Thu, 4 Jun 1998 02:52:08 -0400*References*: <199806030620.CAA14325@smc.vnet.net.>*Sender*: owner-wri-mathgroup at wolfram.com

Hello Joel, I would suggest the following: test=test/.(({#[[1]], #[[2]], _}->#)& /@ test2) Explanation: You want to replace all the elements which have the same x-,y-coordinates as an element of test2 by that element of test2. So I create a list of replacement rules which replaces all the elements which math the first two elements, whereas the value of the third does not count. This can easily be done by mapping a pure function to test2. Problems only arise, if the elements of test2 are not lists with at least two elements. The solution above works (I have not tested it for long lists, but it should work fine, too): test={{{a, b, c}, {a, c, c}, {3, d, 6}},{{a, b, z}, {a, b,1}, {3, d, 6}}}; test2={{a, b, c1}, {a, c, c2}}; test/.(({#[[1]], #[[2]], _}->#)& /@ test2) gives: {{{a,b,c1},{a,c,c2},{3,d,6}},{{a,b,c1},{a,b,c1},{3,d,6}}} I hope, I could help you Reinhold Joel Cannon wrote: > Cam someone suggest better ways to accomplish the following? > > I have two lists containing triplets--test is a rectangular array of > triplets {x,y,z} (e.g. Dimensions {4,40,3}) and test2 is an array of > triplets (e.g. Dimensions {50,3}). test2 is to replace equivalent > elements in test (when the x and y values are equal--the test2 z values > have been calculated more accurately). > > The task can be understood to be: > > 1. Given a triplet {x_i,y_i,z_i} from test2, find the position in test > which has the same {x_i,y_i,_}. > > 2. Replace the element in test by the triplet from test2. > > I resorted to a loop after I decided that it would take me too much time > to figure out a better, more elegant solution. Here is what I did: > > For[i=1,i<=Length[test2],i++, > test = ReplacePart[test,test2[[i]], > test//Position[#,{test2[[i]][[1]],test2[[i]][[2]],_}]& //Flatten]]; -- ____________________________________________________________________ Reinhold Kainhofer | e-mail: reinhold.kainhofer at kfunigraz.ac.at Strassoldog. 4/3/17 | http://www.sbox.tu-graz.ac.at/home/kainhofe/ 8010 Graz, Austria | http://www.geocities.com/Athens/9145/

**References**:**Efficient way to merge lists--Programming help***From:*Joel Cannon <cannon@alpha.centenary.edu>