I have a counter that looks a bit like this:
Counter: {('A': 10), ('C':5), ('H':4)}
I want to sort on keys specifically in an alphabetical order, NOT by counter.most_common()
is there any way to achieve this?
Counting Sort Algorithm. In this tutorial, you will learn about the counting sort algorithm and its implementation in Python, Java, C, and C++. Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array.
Counter is a subclass of dict that's specially designed for counting hashable objects in Python. It's a dictionary that stores objects as keys and counts as values. To count with Counter , you typically provide a sequence or iterable of hashable objects as an argument to the class's constructor.
Just use sorted:
>>> from collections import Counter >>> counter = Counter({'A': 10, 'C': 5, 'H': 7}) >>> counter.most_common() [('A', 10), ('H', 7), ('C', 5)] >>> sorted(counter.items()) [('A', 10), ('C', 5), ('H', 7)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With