Counters.Counter
— TypeA Counter
is a device for keeping a count of how often we observe various objects. It is created by giving a type such as c=Counter{String}()
.
Counts are retrieved with square brackets like a dictionary: c["hello"]
. It is safe to retrieve the count of an object never encountered, e.g., c["goodbye"]
; in this case 0
is returned.
Counts may be assigned with c[key]=amount
, but the more likely use case is using c[key]+=1
to count each time key
is encountered.
Base.:+
— MethodIf c
and d
are Counter
s, then c+d
creates a new Counter
in which the count associated with an object x
is c[x]+d[x]
.
Base.collect
— Methodcollect(C)
for a Counter
returns an array containing the elements of C
each repeated according to its multiplicty.
Base.hash
— FunctionPerforming hash
on a Counter
will first apply clean!
to the Counter
in order that equal Counter
objects hash the same.
Base.keys
— Methodkeys(c::Counter)
returns an interator for the things counted by c
.
Base.length
— Methodlength(c::Counter)
gives the number of entries monitored by the Counter. Conceivably, some may have value 0
.
See also: nnz
.
Base.sum
— Methodsum(c::Counter)
gives the total of the counts for all things in c
.
Base.values
— Methodvalues(c::Counter)
returns an iterator for the counts in c
.
Counters.clean!
— Methodclean!(c)
removes all keys from c
whose value is 0
. Generally, it's not necessary to invoke this unless one suspects that c
contains a lot of keys associated with a zero value.
Counters.collect_by_counts
— Methodcollect_by_counts(C::Counter)
Return a list of the elements counted by C
in which the element with the highest count is first, the element with the second highest count is second, and so forth. Elements whose count is zero are excluded.
Counters.counter
— Methodcounter(list)
creates a Counter
whose elements are the members of list
with the appropriate multiplicities. This may also be used if list
is a Set
or an IntSet
(in which case multiplicities will all be 1).
Counters.csv_print
— Methodcsv_print(C::Counter)
prints out C
in a manner suitable for import into a spreadsheet.
Counters.incr!
— Methodincr!(c,x)
increments the count for x
by 1. This is equivalent to c[x]+=1
.
incr!(c,items)
is more useful. Here items
is an iterable collection of keys and we increment the count for each element in items
.
incr!(c,d)
where c
and d
are counters will increment c
by the amounts held in d
.
Counters.mean
— Methodmean(C::Counter)
computes the weighted average of the objects in C
. Of course, the counted objects must be a Number
; their multiplicity (weight) in the average is determined by their C
-value.
SparseArrays.nnz
— Methodnnz(c::Counter)
gives the number of keys in the Counter
with nonzero value.
See also: length
.