Re: List Comparison
- To: mathgroup at smc.vnet.net
- Subject: [mg67158] Re: [mg67141] List Comparison
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 11 Jun 2006 02:17:16 -0400 (EDT)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
list1={{1,aa,565},{1,bb,566},{1,cc,546}, {2,dd,56},{2,ff,56},{2,ss,5}}; list2={{1,aa,34},{1,bb,66},{1,cc,5443}, {2,dd,76},{2,ff,87},{2,mm,87}}; pairs=Thread[{list1,list2}]; result1=If[Take[#[[1]],2]===Take[#[[2]],2], Append[#[[1]],Last[#[[2]]]]]&/@ pairs/.Null:>Sequence[] {{1, aa, 565, 34}, {1, bb, 566, 66}, {1, cc, 546, 5443}, {2, dd, 56, 76}, {2, ff, 56, 87}} result2=If[Not[Take[#[[1]],2]===Take[#[[2]],2]], #[[1]]]&/@pairs/.Null:>Sequence[] {{2, ss, 5}} result3=If[Not[Take[#[[1]],2]===Take[#[[2]],2]], #[[2]]]&/@pairs/.Null:>Sequence[] {{2, mm, 87}} In version 5.1 or later you can use Pick for result 2 and 3 result2=Pick[list1, Not[Take[#[[1]],2]=== Take[#[[2]],2]]&/@pairs] {{2, ss, 5}} result3=Pick[list2, Not[Take[#[[1]],2]=== Take[#[[2]],2]]&/@pairs] {{2, mm, 87}} Bob Hanlon ---- LectorZ <lectorz at mail.ru> wrote: > Dear experts, > > I want to compare two lists containing sublists and make out of them 3 > result lists. > The comparison applies to the first two elements of the sublists. > > list1 = {{1, aa, 565}, {1, bb, 566}, { > 1, cc, 546}, {2, dd, 56}, {2, ff, 56}, {2, ss, 5}} > list2 = {{1, aa, > 34}, {1, bb, 66}, {1, cc, 5443}, {2, dd, 76}, {2, ff, 87}, {2, mm, > 87}} > > Result ONE should be: > > If the FIRST element AND the SECOND elements of the sublists are equal, > then prepend the THIRD elements from list2. > > result1={{1, aa, 565, 34}, {1, bb, 566, 66}, { > 1, cc, 546, 5443}, {2, dd, 56, 76}, {2, ff, 56, 87}} > > Result TWO should be: > the FIRST element OR the SECOND elements of the sublists are NOT equal, > then display the difference from the list1 perspective > > result2={{2, ss, 5}} > > Result THREE should be: > the FIRST element OR the SECOND elements of the sublists are NOT equal, > then display the difference from the list2 perspective > result3={{2, mm, 87}} > > Thanks for your help > > LZ > -- Bob Hanlon hanlonr at cox.net