Re: Need a functional process for this.
- To: mathgroup at smc.vnet.net
- Subject: [mg55598] Re: Need a functional process for this.
- From: Curt Fischer <tentrillion at gmail.NOSPAM.com>
- Date: Wed, 30 Mar 2005 05:29:05 -0500 (EST)
- References: <d2dpdm$lsd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Steve Gray wrote:
> 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
Here's my method. It creates a pure function
Position[bb,{x_,#}|{#,y_}]& that it maps onto the list aa. This gives a
list of the positions to be eliminated, but if a given element of aa
doesn't cause any eliminations, this mapping leaves an unwanted empty
sublist {} as an element of our larger list. The replacement rule
//.{x___,{},y___}->{x,y} gets rid of those.
I have a feeling there is a better solution that involves DeleteCases[],
but I couldn't come up with anything off the bat.
In[1]:=
aa={2,5,7,9,11};
In[2]:=
bb={{6,4},{9,2},{5,6},{3,8}};
In[3]:=
bb=Delete[bb,(Flatten@Position[bb,{x_,#}|{#,y_}]&/@aa)//.{x___,{},y___}->{x,y}]
Out[3]=
{{6,4},{3,8}}
--
Curt Fischer