Answer: Sorting a counter (and extracting the most common values)

pancy

Pan Chasinga

Posted on September 5, 2019

Answer: Sorting a counter (and extracting the most common values)

To update this question, I'm using Julia v1.2.1 and there is no select! afaik.

Here is a slight modification to @Toivo's implementation I used:

using DataStructures

most_common(c::Accumulator) = most_common(c, length(c))
most_common(c::Accumulator, k) = sort(collect(c), by=kv->kv[2], rev=true)[1:k]

This returns an ::Array{Pair{String,Int}},1 with k members which is similar to Python Counter.most_common(k) which…

💖 💪 🙅 🚩
pancy
Pan Chasinga

Posted on September 5, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related