Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukeuke/pythonize.counter.net
Acts like Counter from python!
https://github.com/lukeuke/pythonize.counter.net
counter nuget nuget-package python pythonize
Last synced: 4 months ago
JSON representation
Acts like Counter from python!
- Host: GitHub
- URL: https://github.com/lukeuke/pythonize.counter.net
- Owner: Lukeuke
- License: mit
- Created: 2024-04-02T01:40:43.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-08-22T17:51:01.000Z (6 months ago)
- Last Synced: 2024-10-09T11:04:55.844Z (4 months ago)
- Topics: counter, nuget, nuget-package, python, pythonize
- Language: C#
- Homepage: https://www.nuget.org/packages/Pythonize.Counter.Net
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Pythonize.Counter.Net
![Tests](https://github.com/Lukeuke/Pythonize.Counter.Net/actions/workflows/dotnet.yml/badge.svg)
![Build](https://github.com/Lukeuke/Pythonize.Counter.Net/actions/workflows/build-only.yml/badge.svg)## Description
Pythonize.Counter.Net is a C# library that provides a Python-like Counter
## Installation
To use Pythonize.Counter.Net in your project, you can install it via NuGet Package Manager:
```bash
Install-Package Pythonize.Counter.Net
```## How-to-use
>obviously like collections.CounterA Counter is a Dictionary for couting objects. Keys are elements and Values are counts.
### Counter Class
The `Counter` 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` class with the specified enumerable of items.```csharp
var value = "abacaba";
var counter = new Counter(value);
```#### Dictionary Get()
Gets the counts of items.
```csharp
var counter = new Counter(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var counts = counter.Get();
// Output:
// { ["apple": 2], ["banana": 3], ["orange": 1] }
```#### int Total()
Calculate the sum of the counts.
```csharp
var counter = new Counter(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var total = counter.Total();
// Output:
// 6
```#### IEnumerable Elements()
Returns an enumerable of elements based on the counts of items.
```csharp
var counter = new Counter(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var elements = counter.Elements();
// Output:
// { "apple", "banana", "apple", "orange", "banana", "banana" }
```#### IEnumerable> MostCommon(int count)
Returns the specified number of most common elements along with their counts from the dictionary.
```csharp
var counter = new Counter(new[] { "apple", "banana", "apple", "orange", "banana", "banana" });
var elements = counter.MostCommon(2);
// Output:
// { ("banana": 3), ("apple": 2) }
```### Exceptions
- `ArgumentNullException`: thrown when the `value` parameter is null.
- `ArgumentOutOfRangeException`: thrown when the `count` parameter is less than 1.## Changelog
See all changes with versions [Here](https://github.com/Lukeuke/Pythonize.Counter.Net/blob/main/CHANGELOG.md)## License
This library is licensed under the MIT License. See the [LICENSE](https://github.com/Lukeuke/Pythonize.Counter.Net/blob/main/LICENSE) file for details.