# Can we select from a list of lists that match from a second list?

Posted 9 years ago
4383 Views
|
3 Replies
|
1 Total Likes
|
 Hi everyoneLets say I have these two listsv = {31,39,39,39,41,39,43,45,45}t = {{2,3,26},{2,18,19},{3,14,22},{4,12,23},{6,13,22},{7,8,24},{8,15,20},{12,16,17},{13,14,18}}The numbers in list v are the totals of the triples in list t in corresponding order.  I am wanting to extract from 't' only those sets of 3 that have multiple occurances in v, so I would wish to extract from t;-  {{2,18,19},{3,14,22},{4,12,23},{7,8,24}, and {12,16,17},{13,14,18}}I have tried using Select with no success, the closest I get is using Cases but with undesired results.  For exampleCases[t, {a_, b_, c_} -> Total[a + b + c] == 39]gives me this{False, True, True, True, False, True, False, False, False}Any help would be appreciatedPaul.
3 Replies
Sort By:
Posted 9 years ago
 Your result from Cases can be used by PickIn[ ]:= bool = Cases[t, {a_, b_, c_} :> MemberQ[{39, 45}, a + b + c]]Out[ ]= {False, True, True, True, False, True, False, True, True}In[ ]:= Pick[t, bool]Out[ ]= {{2, 18, 19}, {3, 14, 22}, {4, 12, 23}, {7, 8, 24}, {12, 16, 17}, {13, 14, 18}}The undesired results from Cases can be avoided by useing Conditional /; on your patterns, to keep the match in tactIn[ ]:= Cases[t, {a_, b_, c_} /; MemberQ[{39, 45}, a + b + c]]Out[ ]= {{2, 18, 19}, {3, 14, 22}, {4, 12, 23}, {7, 8, 24}, {12, 16, 17}, {13, 14, 18}}
Posted 9 years ago
 You can also use Select-Select[GatherBy[t, Total[#] &], Length[#] >= 2 &]{{{2, 18, 19}, {3, 14, 22}, {4, 12, 23}, {7, 8, 24}}, {{12, 16, 17}, {13, 14, 18}}}
Posted 9 years ago
 It seems I jumped the gun in this question, I found Position and Extract worked just fine, all's well.