Re: Need a functional process for this.
- To: mathgroup at smc.vnet.net
- Subject: [mg55618] Re: [mg55589] Need a functional process for this.
- From: János <janos.lobb at yale.edu>
- Date: Thu, 31 Mar 2005 01:24:22 -0500 (EST)
- References: <200503300822.DAA21909@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Mar 30, 2005, at 3:22 AM, 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
I do not know if it is good or not but it is somewhat functional and
100% pedagogical :)
In[1]:=
aa = Table[Random[Integer,
{0, 9}], {i, 1, 10}]
Out[1]=
{8, 6, 9, 7, 9, 8, 5, 1, 9, 1}
In[2]:=
bb = Table[{Random[Integer,
{0, 9}], Random[Integer,
{0, 9}]}, {i, 1, 10}]
Out[2]=
{{9, 0}, {3, 3}, {9, 7},
{2, 8}, {3, 3}, {3, 4},
{3, 1}, {8, 5}, {4, 9},
{5, 2}}
In[42]:=
Intersection[Pick[bb,
Table[Position[aa,
bb[[All,2]][[i]]] ==
{}, {i, 1, Length[
aa]}]], Pick[bb,
Table[Position[aa,
bb[[All,1]][[i]]] ==
{}, {i, 1, Length[aa]}]]]
Out[42]=
{{3, 3}, {3, 4}}
János
----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)
- References:
- Need a functional process for this.
- From: Steve Gray <stevebg@adelphia.net>
- Need a functional process for this.