Python Glossary Русский

all Python 3operatorfunction

operator.countOf

sequences

The number of occurrences of b in a.

Syntax

operator.countOf(a, b)

It works on any iterable, not just lists: a generator or a file will do, but will be consumed. Comparison checks identity first, then ==, so one and the same NaN object counts as equal to itself. To count many values at once a single pass of collections.Counter beats repeated countOf calls.

Examples

import operator
print(operator.countOf([1, 2, 2, 3], 2))   # → 2
print(operator.countOf('abracadabra', 'a'))   # → 5
print(operator.countOf([1, 2, 3], 9))   # → 0
print(operator.countOf({'x': 1, 'y': 1}.values(), 1))   # → 2
print(operator.countOf('abracadabra', 'ab'))   # → 0

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy