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.
Help,
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.
Help,
Hi,
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
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.
Thank you, however a quick question.
Which variable is the pure fuction taking as #?
The pure function
!MemberQ[B, #] &
is applied to each element (#) of A. In the above example, it will be a, b, c, d.
Maybe this SE post can help.