2
$\begingroup$

I start from the following exemplary list, e.g.,

list = {{1, 2, 2}, {3, 2, 4}, {3, 5, 2}, {4, 5, 6}, {2, 5, 6}, {2, 5, 
    2}};

I learned how to to pick out elements of a specific column that fulfill a certain criterion by

In:  Select[list, #[[3]] > 2 &]
Out: {{3, 2, 4}, {4, 5, 6}, {2, 5, 6}}

Now I want to have the values and positions in the original list. I tried something like

In: Flatten[Position[list, _?(#[[3]] > 2 &)]]
Out: {2, 4, 5}

which actually gives me what I want, however with some error messages

"Part specification List[[3]] is longer than depth of object."

What is the correct way to do the pattern matching with Position? Certainly, I could ignore the error messages with Quiet.

Thank you for your help!

$\endgroup$
2
$\begingroup$
Flatten[Position[list, _?(#[[3]] > 2 &), 1, Heads -> False]]

{2, 4, 5}

| improve this answer | |
$\endgroup$
  • $\begingroup$ Are you still using v9? Do you have any ideas for this problem? $\endgroup$ – Szabolcs Nov 28 '17 at 15:54
  • $\begingroup$ @Szabolcs, still on v9:( $\endgroup$ – kglr Nov 28 '17 at 17:54
1
$\begingroup$

Try this:

Position[list, {x_, y_, z_} /; z > 2] // Flatten

(*  {2, 4, 5}  *)

Have fun!

| improve this answer | |
$\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.