Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: List Comparison

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67171] Re: List Comparison
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Sun, 11 Jun 2006 02:18:00 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 6/10/06 at 4:53 AM, lectorz at mail.ru (LectorZ) wrote:

>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}}

This can be obtained as follows:

In[21]:=
common=Intersection[Most/@list1,Most/@list2];
DeleteCases[MapThread[If[MemberQ[common,Most@#1]&&MemberQ[common,Most@#2],\
Flatten@{#1,Last@#2},{}]&,{list1,list2}],{}]

Out[22]=
{{1, aa, 565, 34}, {1, bb, 566, 66}, {1, cc, 546, 5443}, 
  {2, dd, 56, 76}, {2, ff, 56, 87}}

Note, this method assumes both lists are the same length and both have the common elements in the same positions. The second restriction can be removed by sorting the lists

>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}}

In[23]:=
Select[list1, !MemberQ[common,Most@#]&]

Out[23]=
{{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}}

In[24]:=
Select[list2, !MemberQ[common,Most@#]&]

Out[24]=
{{2, mm, 87}}
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Getting Mathematica CellTags to export to TeX?
  • Next by Date: Re: List Comparison
  • Previous by thread: Re: List Comparison
  • Next by thread: Re: List Comparison