This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / Programming / Pattern Matching  /
Count

  • Count[ list , pattern ] gives the number of elements in list that match pattern.
  • Count[ expr , pattern , levelspec ] gives the total number of subexpressions matching pattern that appear at the levels in expr specified by levelspec.
  • Level specifications are described in Section¬†A.3.6.
  • See the Mathematica book: Section 1.8.5,¬†Section 2.3.2.
  • See also: FreeQ, MemberQ, Cases, Select, Position.
  • Related package: Statistics`DataManipulation`.

    Further Examples

    (See the Further Examples for FullSimplify for a demonstration of how Count can be used to affect the outcome of a simplification.)
    This counts the number of occurrences of Real numbers in the list. (An Integer is not considered to be Real, any more than a Real is considered to be Complex.)

    In[1]:=

    Out[1]=

    This command returns the number of summands that are powers. (Note that the first argument to Count need not be a list.)

    In[2]:=

    Out[2]=

    This returns the number of expressions that occur in the sum at any level and are powers. This count is higher; the actual matches can be seen by replacing Count with Cases.

    In[3]:=

    Out[3]=