Message Boards Message Boards

5 Replies
1 Total Likes
View groups...
Share this post:

Very simple question with MemberQ

Posted 11 years ago
I want to make a function that will take list A, match it with list B, and delete any non-matching elements in list A.

I imagine that this will be very easy to accomplish using MemberQ, however, I cannot think up a way.

POSTED BY: Seokin Yeh
5 Replies
Maybe this SE post can help.
POSTED BY: Rodrigo Murta
The pure function
!MemberQ[B, #] &
is applied to each element (#) of A. In the above example, it will be a, b, c, d.
POSTED BY: Youngjoo Chung
Thank you, however a quick question.

Which variable is the pure fuction taking as #?
POSTED BY: Seokin Yeh
Depending on the exact usage and how repeated elements should be treated, functions like Complement and DeleteCases might also be helpful. Take a look at this thread for some interesting examples.
POSTED BY: Ilian Gachevski

Try the following:
fun[A_List, B_List] := Select[A, ! MemberQ[B, #] &];
fun[{a, b, c, d}, {c, d, e, f}]

Out[]= {a, b}

Youngjoo Chung
POSTED BY: Youngjoo Chung
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract