5
$\begingroup$

I have a large list of arrays of the following form

{{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2}, 
  0}, {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2}, 
  0}, {{2, -2, 0}, 0}, {{2, -2, -2}, 0}}

with the second entity in each element is either 0 or 1. I need to create a do loop that search within each array for those elements with second entity equals 1 and return the result as a new array with all other elements eliminated. If none of the array element satisfy this requirement I need the result to be returned as 0. For example for the above array I want to get the result as

{{0, 0, 0}, {2, 0, 0}} 

because these two elements have second entity equals 1.. How can I do that?

$\endgroup$

closed as off-topic by Bob Hanlon, MarcoB, user9660, ciao, István Zachar Jun 24 '16 at 12:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, MarcoB, Community, ciao, István Zachar
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ No need for loops, use either Cases[] or Select[]. Alternatively, use GatherBy[] or GroupBy[] and pick out what you need. $\endgroup$ – J. M. will be back soon Jun 22 '16 at 16:21
  • $\begingroup$ Cases[lst, {a_List, 1} :> a] $\endgroup$ – march Jun 22 '16 at 16:22
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jun 22 '16 at 17:49
7
$\begingroup$

Never forget Pick for problems involving picking elements from a list.

data = 
  {{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2}, 0}, 
   {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2}, 0}, 
   {{2, -2, 0}, 0}, {{2, -2, -2}, 0}};
Pick[data, Last /@ data, 1]

{{{0, 0, 0}, 1}, {{2, 0, 0}, 1}}

$\endgroup$
  • $\begingroup$ Or, without Map: Pick[#, #[[All, 2]], 1] &@data $\endgroup$ – user1066 Jun 22 '16 at 21:08
5
$\begingroup$
Cases[{{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2}, 
   0}, {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2}, 
   0}, {{2, -2, 0}, 0}, {{2, -2, -2}, 0}}, {list_, 1} :> list]

(*{{0, 0, 0}, {2, 0, 0}}*)
$\endgroup$
  • $\begingroup$ Thanks for the quick reply.. What if I want to get 0 if none of the elements satisfy the condition? $\endgroup$ – eftrsd Jun 22 '16 at 16:26
  • 1
    $\begingroup$ Just add /. {} -> 0 at the end. $\endgroup$ – march Jun 22 '16 at 16:33
  • $\begingroup$ Brilliant! Thanks $\endgroup$ – eftrsd Jun 22 '16 at 16:58
4
$\begingroup$

Example

Data

list = {{{0, 0, 0}, 1}, {{2, 2, 2}, 0}, {{2, 2, 0}, 0}, {{2, 2, -2}, 0}, {{2, 0, 2}, 0}, {{2, 0, 0}, 1}, {{2, 0, -2}, 0}, {{2, -2, 2}, 0}, {{2, -2, 0}, 0}, {{2, -2, -2}, 0}}

Code

Select[list, #[[2]] == 1 &][[All, 1]] (*For cases 1*)
Select[list, #[[2]] == 2 &][[All, 1]] (*For cases 2*)

Output

{{0, 0, 0}, {2, 0, 0}}

Reference

Part
Select

$\endgroup$
3
$\begingroup$

You can also use replacement rules. FYI, this method is usually slow on very large lists.

list/. {
  {{_, _, _}, 0} -> Sequence[],
  {x : {_, _, _}, 1} :> x
  }

Which gives:

{{0, 0, 0}, {2, 0, 0}}
$\endgroup$
2
$\begingroup$

Just some variants.

If only want first element:

Reap[Sow @@@ data, 1, #2 &][[2, 1]]
1 /. GroupBy[data, Last -> First]

both yield:

{{0, 0, 0}, {2, 0, 0}}

If whole element:

1 /. GroupBy[data, Last]

yields:

{{{0, 0, 0}, 1}, {{2, 0, 0}, 1}}
$\endgroup$

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