MathGroup Archive 1998

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

Search the Archive

Re: Efficient way to merge lists--Programming help

Hello Joel,
I would suggest the following:

test=test/.(({#[[1]], #[[2]], _}->#)& /@ test2)

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:


I hope, I could help you

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
Strassoldog. 4/3/17 | 8010
Graz, Austria  |

  • Prev by Date: Re: Efficient way to merge lists--Programming help
  • Next by Date: automatic numbering and referencing
  • Previous by thread: Re: Efficient way to merge lists--Programming help
  • Next by thread: Re: Efficient way to merge lists--Programming help