spiketools.utils.base.count_elements

spiketools.utils.base.count_elements(data, labels=None, sort=False)[source]

Count elements within a collection object.

Parameters:
datalist or 1d array

The data to count elements of.

labelslist of str or ‘count’, optional

Labels to ensure are in the counter, adding a count of zero if missing. If list, should be a list of labels to check. If ‘count’, then adds labels for integer count values across the observed range.

sortbool, optional, default: False

Whether to sort the counter, by key, before returning.

Returns:
countsCounter

Counts of the elements within the given data object.

Examples

Count the number of occurrences of each element in a 1d array:

>>> data = [1, 3, 3, 4, 5, 6, 9, 3, 4, 5, 6]
>>> count_elements(data)
Counter({3: 3, 4: 2, 5: 2, 6: 2, 1: 1, 9: 1})