3
$\begingroup$

I have a list of pairs for example like following:

list={{1,0.001},{10^(-7),0.001},{0.01,0.00005},{10^(-11),10^(-4)}}

Now I would like to delete all pairs (the complete pair), where the first element of the pair is smaller than 10^(-6). I tried some complicated combination of a For-Next loop and an If condition but without success. Is there an elegant way of doing this?

Luke

edit: extension: How would you do it, if I would have a list of lists of pairs? (Again delete the complete pair if the first element is smaller than some number)

$\endgroup$

marked as duplicate by march, Mr.Wizard list-manipulation Feb 19 '16 at 23:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ DeleteCases[list, {a_, _} /; a <= 10^(-6)] $\endgroup$ – march Feb 19 '16 at 17:41
  • 3
    $\begingroup$ Select[list, First@# > 10^(-6) &] $\endgroup$ – march Feb 19 '16 at 17:42
  • 1
    $\begingroup$ GroupBy[list, First@# > 10^(-6) &][True] $\endgroup$ – march Feb 19 '16 at 17:44
  • $\begingroup$ Wow mathematica is amazing :) Thank you alot! How would you extend this if I would have a list of lists of pairs? (again delete the pairs if the first element is smaller than 10^(-5)) $\endgroup$ – Luke Feb 19 '16 at 17:45
  • $\begingroup$ list /. {a_ /; a <= 10^(-6), _} :> (## &[]) $\endgroup$ – march Feb 19 '16 at 17:47
7
$\begingroup$

An easy new way (since Mathematica 10.2) to remove some part from a list or expression is to replace that part with Nothing

For example you can do

list = {{1, 0.001}, {10^(-7), 0.001}, {0.01, 0.00005}, {10^(-11), 
    10^(-4)}};
list /. {a_ /; a <= 10^-6, _} -> Nothing

to get:

{{1, 0.001}, {0.01, 0.00005}}

About the syntax:

  • /. is ReplaceAll
  • ... -> ... is Rule
  • {a_ /; a <= 10^-6, _} is a pattern with a Condition; you can read it "any pair with some first element/expression (say a) such that a<=10^-6 and any second element (_ is Blank, any expression)"

The good (or, sometimes, maybe the bad) about this method is that easily translates to the "list of list (of list of list...) of pair". You don't need to change anything:

{list, list, list} /. {a_ /; a <= 10^(-6), _} -> Nothing

{{{1, 0.001}, {0.01, 0.00005}}, {{1, 0.001}, {0.01, 0.00005}}, {{1, 0.001}, {0.01, 0.00005}}}

This method it's just a variant of the "fun" approach of @march, but with an easy-to-understand (?) syntax, available since v10.2. Under a previous you still need to use an equivalent approach:

... /. {a_ /; a <= 10^(-6), _} -> Sequence[]
... /. {a_ /; a <= 10^(-6), _} -> (##&[]) (as of @march comment)

Using Nothing, Sequence[], or (##&[]) and :> or -> is not always the same, but, for this sample, it is.


I also add another way:

Pick[list, UnitStep[list[[All, 1]] - 10^-6], 1]

{{1, 0.001}, {0.01, 0.00005}}

not useful in the list of list of pair scenario and not exactly equivalent to the previous, but can have some other advantage:

list2 = Flatten[Table[list, {10000}], 1];
Pick[list2, UnitStep[list2[[All, 1]] - 10^-6], 1] // 
  RepeatedTiming // First
list2 /. {a_ /; a <= 10^-6, _} -> Nothing // RepeatedTiming // First

0.0338

0.0580

$\endgroup$
  • $\begingroup$ The way to do it pre-10.2 is list /. {a_ /; a <= 10^(-6), _} :> (## &[]), as in my comment above. I appreciate you writing it out in detail. +1. $\endgroup$ – march Feb 19 '16 at 22:14

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