2
$\begingroup$

So lets say I create a list of pairs like so:

list = {{1, 100}, {2, 10000}, {3, 100400}, {4,50000}, {5,2000}}

Now, I want to find what entries satisfy the condition where the second number is larger than 10000. The code I have tried is as follows:

Select[list[[All, 2]], list[[All, 2]] > 10000]

I think I understand why the above code simply returns an empty set each time. In the conditional, it compares the entire list of the second column numbers to 10000, which does not make any sense. I want the conditional/criterion to compare each number in the second column of the list to 10000 separately, and then have the Select function return which values were true. Any ideas for how to do this?

$\endgroup$
  • 3
    $\begingroup$ @Nasser's Cases and Select are great for ease-of-use. But you could also use something involving Pick, like Pick[#, UnitStep /@ (#[[;; , 2]] - 1000), 1] &@list, which can sometimes speed things up for long lists. $\endgroup$ – aardvark2012 Oct 18 '17 at 3:12
  • $\begingroup$ @aardvark2012 UnitStep is listable ;) $\endgroup$ – LLlAMnYP Oct 18 '17 at 13:28
  • $\begingroup$ @LLlAMnYP Heh. Of course. Good answer! :-) $\endgroup$ – aardvark2012 Oct 18 '17 at 23:32
5
$\begingroup$
list = {{1, 100}, {2, 10000}, {3, 100400}, {4, 50000}, {5, 2000}}
Cases[list,{_,y_}/;y>1000]

Mathematica graphics

To use Select

Select[list,#[[2]]>1000&]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Looks like that made it work. Thank you so much for your help! $\endgroup$ – BirdsRule123 Oct 18 '17 at 1:32
3
$\begingroup$

For completeness, the high-performance solution, similar to the comment by aardvark2012:

Pick[list, UnitStep[10000 - list[[;; , 2]]], 0]

A performance test:

list = RandomInteger[30000, {10^6, 2}];
(pick = Pick[list, UnitStep[10000 - list[[;; , 2]]], 0]) // AbsoluteTiming // First
(cases = Cases[list, {_, y_} /; y > 10000]) // AbsoluteTiming // First
(select = Select[list, #[[2]] > 10000 &]) // AbsoluteTiming // First
(pickUbq = Pick[list, #[[2]] > 10000 & /@ list]) // AbsoluteTiming // First
pick == cases == select == pickUBQ
0.050378
0.926318
1.72531
2.04373
True
$\endgroup$
1
$\begingroup$

Just example using:

Pick:

Pick[list, #[[2]] > 1000 & /@ list]

and (more contrived) Reap/Sow:

Join @@ Reap[Sow[{##}, #2] & @@@ list, _?(# > 1000 &), #2 &][[-1]]
$\endgroup$
  • $\begingroup$ Something seems wrong with the Reap/Sow solution. Is it giving sorted results? $\endgroup$ – LLlAMnYP Oct 18 '17 at 13:23
  • $\begingroup$ @LLlAMnYP thank you ...will look at when I get a chance to sort out my error but it 23:30 in my TZ... $\endgroup$ – ubpdqn Oct 18 '17 at 13:26
  • $\begingroup$ After inspection: As per my answer pick // Sort == yourReapSow // Sort. You're just sowing the list with tags equal to the second element, so of course they come in a different order. Otherwise same result. $\endgroup$ – LLlAMnYP Oct 18 '17 at 13:31

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.