RE: List Comparison
- To: mathgroup at smc.vnet.net
- Subject: [mg67179] RE: [mg67141] List Comparison
- From: "David Park" <djmp at earthlink.net>
- Date: Sun, 11 Jun 2006 02:18:36 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
I don't know what you mean by "the difference from the list1 perspective" and my result two does not agree with your sample result. Nevertheless, straightening up the definition if I used the wrong one, you can proceed as follows... 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}}; result1[{a1_, b1_, c1_}, {a2_, b2_, c2_}] := If[a1 === a2 && b1 === b2, {a1, b1, c1, c2}, Unevaluated[Sequence[]]] result2[{a1_, b1_, c1_}, {a2_, b2_, c2_}] := If[(a1 =!= a2 || b1) =!= b2, {a1, b1, c1 - c2}, Unevaluated[Sequence[]]] result3[{a1_, b1_, c1_}, {a2_, b2_, c2_}] := If[(a1 =!= a2 || b1) =!= b2, {a2, b2, c2 - c1}, Unevaluated[Sequence[]]] MapThread[result1, {list1, list2}] {{1, aa, 565, 34}, {1, bb, 566, 66}, {1, cc, 546, 5443}, {2, dd, 56, 76}, {2, ff, 56, 87}} MapThread[result2, {list1, list2}] {{2, ss, -82}} MapThread[result3, {list1, list2}] {{2, mm, 82}} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: LectorZ [mailto:lectorz at mail.ru] To: mathgroup at smc.vnet.net 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