I want the in-built function Count
to count the elements in a list which are greater than a value.
Example:
Count[{1, 1, 2, 3}, (# > 1.5) &]
Why doesn't this work?
I want the in-built function Count
to count the elements in a list which are greater than a value.
Example:
Count[{1, 1, 2, 3}, (# > 1.5) &]
Why doesn't this work?
Your second argument is a function instead of a pattern.
Count[{1, 1, 2, 3}, _?(# > 1.5 &)]
Select
is quite close to Count
by its functionality. However, it recognizes the pattern argument in the form given in the above question, that is, just #>1.5&
as follows: Select[{1, 1, 2, 3}, (# > 1.5 &)]
returns {2, 3}
. What is the difference between these two cases?
$\endgroup$
Commented
Feb 9, 2016 at 9:34
Count[{{1,2},3,4}, _Integer, 2]
. This cannot be done with Select, that works only on level 1.
$\endgroup$
Commented
Feb 10, 2016 at 9:18
In V10.0+ you can stick with functions:
CountsBy[{1, 1, 2, 3}, (# > 1.5) &][True]