CountsBy

CountsBy[{e1,e2,},f]

gives an association whose keys are the distinct values of the f[ei], and whose values give the number of times these f[ei] values appear.

CountsBy[f]

represents an operator form of CountsBy that can be applied to an expression.

Details

  • CountsBy[{e1,e2,},f] gives an association whose keys f[ei] are in the order in which the corresponding ei first appear. »
  • CountsBy[f][expr] is equivalent to CountsBy[expr,f].

Examples

open allclose all

Basic Examples  (2)

Count the even and odd elements of a list:

Count the prime and composite numbers in a list:

Use the operator form of CountsBy:

Scope  (1)

Count elements have the same absolute value:

Count using the operator form:

Properties & Relations  (2)

Elements in the result appear in the order in which values of f[ei] appear in the list:

Reverse the elements of the list, and the order of counts is reversed:

Move zero to the end of the list, and yet another order of counts is generated:

CountsBy[list,f] is effectively Counts[Map[f,list]]:

Wolfram Research (2014), CountsBy, Wolfram Language function, https://reference.wolfram.com/language/ref/CountsBy.html.

Text

Wolfram Research (2014), CountsBy, Wolfram Language function, https://reference.wolfram.com/language/ref/CountsBy.html.

CMS

Wolfram Language. 2014. "CountsBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CountsBy.html.

APA

Wolfram Language. (2014). CountsBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CountsBy.html

BibTeX

@misc{reference.wolfram_2024_countsby, author="Wolfram Research", title="{CountsBy}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/CountsBy.html}", note=[Accessed: 20-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_countsby, organization={Wolfram Research}, title={CountsBy}, year={2014}, url={https://reference.wolfram.com/language/ref/CountsBy.html}, note=[Accessed: 20-January-2025 ]}