Re: Need a functional process for this.
- To: mathgroup at smc.vnet.net
- Subject: [mg55610] Re: [mg55589] Need a functional process for this.
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 31 Mar 2005 01:24:02 -0500 (EST)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
aa={2,5,7,9,11}; bb={{6,4},{9,2},{5,6},{3,8}}; Select[bb, !MemberQ[aa,#[[1]]]&& !MemberQ[aa,#[[2]]]&] {{6, 4}, {3, 8}} This can be simplified with a helper function anyMemberQ[lst_List, forms_List] := Or@@(MemberQ[lst,#]&/@forms); Select[bb, !anyMemberQ[aa,#]&] {{6, 4}, {3, 8}} To handle the situation when aa may be either a list of lists or either a vector or a list of lists, use Flatten aa={{2}, {5}, {7}, {9}, {11}}; Select[bb, !MemberQ[Flatten[aa],#[[1]]]&& !MemberQ[Flatten[aa],#[[2]]]&] {{6, 4}, {3, 8}} Select[bb, !anyMemberQ[Flatten[aa],#]&] {{6, 4}, {3, 8}} Bob Hanlon > > From: Steve Gray <stevebg at adelphia.net> To: mathgroup at smc.vnet.net > Date: 2005/03/30 Wed AM 03:22:03 EST > To: mathgroup at smc.vnet.net > Subject: [mg55610] [mg55589] Need a functional process for this. > > I have two lists, aa and bb. aa has the general form {2,5,7,9,11,...} > (or{{2},{5},{7},{9},{11},?}}), and bb has the general form {{6,4},{9,2},{5,6},{3,8},?}. If either > the first or second element in any sublist (pair of integers) of bb matches any element in aa, I > want to delete that sublist from bb. In the above example, neither member of {6,4} or {3,8} belongs > to aa, while at least one element of {9,2} and {5,6} belongs to aa, so bb becomes {{6,4},{3,8}}. If > aa had only one element, for example 7, I could do bb=Delete[bb,Position[bb,{x_,7}|{7,y_}]], but I > don't know how to do it for several values instead of just "7" without using a procedural loop. > What is a good functional way to do this? > Thank you for any tips. > > Steve Gray > >