Pythonize.Counter.Net is a C# library that provides a Python-like Counter
To use Pythonize.Counter.Net in your project, you can install it via NuGet Package Manager:
Install-Package Pythonize.Counter.Net
obviously like collections.Counter
A Counter is a Dictionary for couting objects. Keys are elements and Values are counts.
The Counter<T>
class is a generic class that counts the occurrences of items in an enumerable or span of items.
Initializes a new instance of the Counter<T>
class with the specified enumerable of items.
var value = "abacaba";
var counter = new Counter<char>(value);
Gets the counts of items.
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var counts = counter.Get();
// Output:
// { ["apple": 2], ["banana": 3], ["orange": 1] }
Calculate the sum of the counts.
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var total = counter.Total();
// Output:
// 6
Returns an enumerable of elements based on the counts of items.
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var elements = counter.Elements();
// Output:
// { "apple", "banana", "apple", "orange", "banana", "banana" }
Returns the specified number of most common elements along with their counts from the dictionary.
var counter = new Counter<string>(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var elements = counter.MostCommon(2);
// Output:
// { ("banana": 3), ("apple": 2) }
ArgumentNullException
: thrown when thevalue
parameter is null.ArgumentOutOfRangeException
: thrown when thecount
parameter is less than 1.
See all changes with versions Here
This library is licensed under the MIT License. See the LICENSE file for details.