8
$\begingroup$

I am new in Wolfram Math and I need a help in making a simple program. Let's suppose we have a list:

list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};

Now, I need to collect the first elements of sublists, but not all of them, only those whose second elements are larger than 8.

Thanks in advance.

$\endgroup$
8
$\begingroup$
list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};

Select[list, #[[2]] > 8 &][[All, 1]]

{1, 7, -1}

OR using Pick

Pick[list[[All, 1]], UnitStep[list[[All, 2]] - 8], 1]

{1, 7, -1}

|improve this answer|||||
$\endgroup$
  • $\begingroup$ @Suro. You're welcome, glad I could help. $\endgroup$ – RunnyKine Apr 18 '16 at 11:43
  • $\begingroup$ Wow! How about, when we say that list has 3 3l3mens in each sublist list = {{1, 11, 3}, {2, 7, 4}, {4, 2, 5}, {7, 9, 9}, {-2, 3, 3}, {-1, 10, 5}};. I would like to impose two conditions and get second and third element which satisfies this condition. How to do it? I wrote a code like this, but I don't know how to use it to get both 2nd and 3rd element: Select[list, #[[2]] > 8 && #[[3]] > 3 &] Select[list, #[[2]] > 8 && #[[3]] > 3 &][[All, 1]] $\endgroup$ – sreeraj t Mar 16 at 15:37
8
$\begingroup$
list = {{1, 11}, {2, 7}, {4, 2}, {7, 9}, {-2, 3}, {-1, 10}};
Cases[list, {a_, b_} /; b > 8 :> a]
(* {1, 7, -1} *)

What I'm doing above is to use Cases to select only those sublists whose second element is greater than 8,

Cases[list, {a_, b_} /; b > 8]
(* {{1, 11}, {7, 9}, {-1, 10}} *)

The /; notation defines a Condition. Then I'm applying a :> to make a replacement, in this case keeping only the first element, look at RuleDelayed

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Wow! Another excellent answer. Now, lets say that we have list which is like this list={{1,11,3},{2,7,4},{4,2,5},{7,9,9},{-2,3,3},{-1,10,5}};. Now I have to take out the second and third value in the sublist which satisfies the criteria b>8 and c>8. I would like to take out the value of b and c which satisfies this criterion. How to do this? I wrote a code, but its not working: Cases[list,{a_,b_,c_}/;b>8:>b] Cases[list,{a_,b_,c_}/;b>8:>c] Cases[list,{a_,b_,c_}/;b>8&c>8:>b] $\endgroup$ – sreeraj t Mar 16 at 15:49
3
$\begingroup$
Pick[list[[All, 1]], # > 8 & /@ list[[All, 2]]]

or

Pick[#[[1]], # > 8 & /@ #[[2]]] &@Transpose[list]

{1, 7, -1}

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Nice one. But, how about this list list={{1,11,3},{2,7,4},{4,2,5},{7,9,9},{-2,3,3},{-1,10,5}}; and taking out 2nd and 3rd element in each sublist which satisfies simultaneously 2 condition such that 2nd and 3rd element should be more than 8? $\endgroup$ – sreeraj t Mar 16 at 15:54
  • 1
    $\begingroup$ @sreerajt, you can try Pick[#, #[[1]] > 8 && #[[2]] > 8 & /@ #] &@list[[All, {2, 3}]] or Select[#[[1]] > 8 && #[[2]] > 8 &]@list[[All, {2, 3}]] or ` Cases[_?(#[[1]] > 8 && #[[2]] > 8 &)]@list[[All, {2, 3}]]`. $\endgroup$ – kglr Mar 16 at 16:23
  • $\begingroup$ many thank 4 dese excellent suggestions. But I do have a pblm in this: take 1st one. In this, what it does is dat it first picks out 2nd and 3rd elements from each sublist by list[[All,{2,3}]] and then apply the condition to check on 1st & 2nd element on this modified list (if you have a doubt you can check it by changing #[[2]] to #[[3]]-it gives an error). How about a case where u check for condition on 1st and 3rd element of each sublist & then select 2nd and 3rd element in d sublist which satisfies this condition? This question applies to all ur sugestions $\endgroup$ – sreeraj t Mar 17 at 1:49
3
$\begingroup$

Mathematica/Wolfram Language provides a lot of flexibility. Some ways you could do this:

Cases[list, {x_, _?(# > 8 &)} :> x]
Select[list, #[[2]] > 8 &][[;; , 1]]
Pick[list, #[[2]] > 8 & /@ list][[;; , 1]]
True /. GroupBy[list, (#[[2]] > 8 &) -> (#[[1]] &)]
Flatten[Last@Reap[Sow @@@ list, _?(# > 8 &), #2 &]]

I encourage you to look the documentation and play. You can find the way that suits your aims and/or style.

and some more ridiculous:

Cases[{#1, Sign[#2 - 8]} & @@@ list, {x_, 1} :> x]
f[x_, y_] := x /; y > 8
f[__] := Sequence[]
f @@@ list
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Your advice is highly appreciated. Thanks. $\endgroup$ – Suro Apr 18 '16 at 11:50

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.