RE: help with lists operations
- To: mathgroup at smc.vnet.net
- Subject: [mg67850] RE: [mg67822] help with lists operations
- From: "David Park" <djmp at earthlink.net>
- Date: Mon, 10 Jul 2006 06:38:22 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
It isn't too clear to me what your exact specifications are. For example, can a pair for the first list match more than one pair from the second list? Or how about two or three pairs from the first list matching two or three pairs from the second list. Are we to take all combinations? If we assume that there will be at most only one pair that matches then the following code will work. Copy and paste into your notebook. f1 = {{2, 1}, {3, 1}, {7, 2}, {9, 1}, {10, 1}, {12, 1}, {14, 3}, {19, 3}, {23, 1}, {24, 1}, {25, 1}}; f2 = {{2, 2}, {5, 1}, {6, 2}, {10, 1}, {14, 3}, {16, 1}, {18, 1}, {21, 3}, {22, 1}, {26, 1}}; Print["Add a 1 or 2 index to each pair and join the two lists"] step1 = Join[Flatten@{##, 1} & /@ f1, Flatten@{##, 2} & /@ f2] Print["Sort the resulting list"] step2 = Sort[step1] Print["Split the list into sublists that have the same first entry"] step3 = Split[step2, First[#1] === First[#2] &] Print["Select groups of two entries which are different in the third item."] step4 = Select[step3, Length[#] == 2 \[And] Part[#, 1, 3] != Part[#, 2, 3] &] Print["Replace by products of second element"] step5 = Part[#, 1, 2]Part[#, 2, 2] & /@ step4 Print["Add the result"] Plus @@ step5 David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: omk [mailto:omk at iname.com] To: mathgroup at smc.vnet.net Hi ALL, I am a novice in Mathematica, know that there are no easy ways for learning, so spent 2 weeks trying to solve an easy problem, but so far have only ugly solutions. Yes, I read and reread the Book as well as searched for similar problems on the Forum. I have 2 lists: f1={{2, 1}, {3, 1}, {7, 2}, {9, 1}, {10, 1}, {12, 1}, {14, 3}, {19, 3}, {23, 1}, {24, 1}, {25, 1}} f2={{2, 2}, {5, 1}, { 6, 2}, {10, 1}, {14, 3}, {16, 1}, {18, 1}, {21, 3}, {22, 1}, {26, 1}} I need to compare sublists (pairs) from the first list f1 with pairs from f2. If pairs with 2 first equal numbers exist, I have to multiply the second, after having products for all such pairs, I need to receive their sum. Say from f1 and f2 I have {2,1} and {2,2}, the product is 1 2=2, similar {10,1} and {10,1},{14,3} and {14,3}, so it should be 2+1+9=12 Any ideas are very welcome TIA, omk