2
$\begingroup$

Lets say I have 4 rows in

alist = {{1, 1, 3, 3, 1, 6}, {1, 1, 3, 4, 1, 1, x}, {1, 2, 3, 3, 1, 
    6}, {1, 2, 3, 4, 1, 1, x}};

If I want to select and print out only the rows with the "x" from the previous one, i.e.,

{{1, 1, 3, 4, 1, 1, x}, {1, 2, 3, 4, 1, 1, x}}

Is there a way to do it?

$\endgroup$
1
  • 1
    $\begingroup$ Thank for clarify the question @Syed $\endgroup$
    – kompaui
    May 21 at 5:31

2 Answers 2

5
$\begingroup$

Using Select:

Select[alist, MemberQ[#, x] &]

Using Pick:

Pick[alist, MemberQ[#, x] & /@ alist]

Using Cases:

Cases[alist, {p___, x, q___}]

Using ContainsAny

Pick[alist, ContainsAny[#, {x}] & /@ alist]

or

Select[ContainsAny[{x}]][alist]

Result:

{{1, 1, 3, 4, 1, 1, x}, {1, 2, 3, 4, 1, 1, x}}
$\endgroup$
2
  • $\begingroup$ This is perfect!!!! Now I can adjust a little bit and get what I want. Thank you very much. $\endgroup$
    – kompaui
    May 21 at 5:42
  • $\begingroup$ Thanks for the accept. I removed Tableform as you can apply it later to the results. Try to use *Form wrappers in general for display purposes only and not during computation. $\endgroup$
    – Syed
    May 21 at 5:44
2
$\begingroup$
Select[alist,EqualTo[x]@*Last]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.