Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9548 > unrolled thread

Intersection problem

Started byCisco Lane <travlorf@yahoo.com>
First post2012-04-10 06:28 +0000
Last post2012-04-11 22:21 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Intersection problem Cisco Lane <travlorf@yahoo.com> - 2012-04-10 06:28 +0000
    Re: Intersection problem Jason Ebaugh <ebaughjason@gmail.com> - 2012-04-11 22:18 +0000
    Re: Intersection problem Murta <rodrigomurtax@gmail.com> - 2012-04-11 22:19 +0000
    Re: Intersection problem Yi Wang <tririverwangyi@gmail.com> - 2012-04-11 22:21 +0000

#9548 — Intersection problem

FromCisco Lane <travlorf@yahoo.com>
Date2012-04-10 06:28 +0000
SubjectIntersection problem
Message-ID<jm0ju7$adk$1@smc.vnet.net>
I need to select out the elements of the first list whose first elements are contained in the second list. For example

first list: {{a,x},{b,y},{c,z}}

second list: {b,c,d}

result: {{b,y},{c,z}}

Is there a quick way to do this? Intersection, maybe somehow?

[toc] | [next] | [standalone]


#9550

FromJason Ebaugh <ebaughjason@gmail.com>
Date2012-04-11 22:18 +0000
Message-ID<jm4vvs$2kt$1@smc.vnet.net>
In reply to#9548
In[31]:= 
firstlist={{a,x},{b,y},{c,z}};
secondlist={b,c,d};

Cases[firstlist, xx_ /; MemberQ[secondlist,xx[[1]]]]

Out[33]= {{b,y},{c,z}}


Jason Ebaugh

Contact: jasonebaugh@jasonebaugh.info
Blog: www.jasonebaugh.info/wordpress/
LinkedIn: www.linkedin.com/in/jasonebaugh

[toc] | [prev] | [next] | [standalone]


#9551

FromMurta <rodrigomurtax@gmail.com>
Date2012-04-11 22:19 +0000
Message-ID<jm500s$2lf$1@smc.vnet.net>
In reply to#9548
On Apr 10, 3:28 am, Cisco Lane <travl...@yahoo.com> wrote:
> I need to select out the elements of the first list whose first elements 
are contained in the second list. For example
>
> first list: {{a,x},{b,y},{c,z}}
>
> second list: {b,c,d}
>
> result: {{b,y},{c,z}}
>
> Is there a quick way to do this? Intersection, maybe somehow?

Hi Lane
There are 2 ways to do that:

l1 = {{a, x}, {b, y}, {c, z}}; l2 = {b, c, d};

number 1
Select[l1, MemberQ[l2, #[[1]]] &]

number 2
Cases[l1, {a_ /; MemberQ[l2, a], _}]

best regards
Murta

[toc] | [prev] | [next] | [standalone]


#9554

FromYi Wang <tririverwangyi@gmail.com>
Date2012-04-11 22:21 +0000
Message-ID<jm504m$2n3$1@smc.vnet.net>
In reply to#9548
There should be many ways to do it. The question is to find a good one. The simplest I can think of is:

first = {{a, x}, {b, y}, {c, z}};
second = {b, c, d};
Select[first, MemberQ[second, #[[1]]] &]

There are also some more complicated realisations, such as

first[[#]] & /@ 
 Flatten@Map[Position[Transpose[first][[1]], #] &, second]

Best,
Yi

On Tuesday, April 10, 2012 2:28:23 AM UTC-4, Cisco Lane wrote:
> I need to select out the elements of the first list whose first elements are contained in the second list. For example
> 
> first list: {{a,x},{b,y},{c,z}}
> 
> second list: {b,c,d}
> 
> result: {{b,y},{c,z}}
> 
> Is there a quick way to do this? Intersection, maybe somehow?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web