Message Boards Message Boards

0
|
4860 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

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

Posted 10 years ago
Hi everyone

Lets say I have these two lists

v = {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 example

Cases[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 appreciated

Paul.
POSTED BY: Paul Cleary
3 Replies
Posted 10 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 BY: Girish Arabale
Posted 10 years ago
It seems I jumped the gun in this question, I found Position and Extract worked just fine, all's well.
POSTED BY: Paul Cleary
Your result from Cases can be used by Pick
In[ ]:= 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 tact
In[ ]:= 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}}
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract