sorting a counter in python by keys
Asked Answered
V

6

55

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?

Vilberg answered 29/7, 2013 at 17:51 Comment(2)
A Counter is basically just a dictionary, so this should really be considered a duplicate of this: #9002009Magnitude
Do you want to print them in a sorted order?Barnsley
L
85

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)]
Lemuel answered 29/7, 2013 at 17:55 Comment(4)
I agree, knowing that the iterator on a dict yields the keys, not the values, then the keys will get sorted.Gavrielle
Note, this transforms Counter to a listNepil
@crypdick, Yes it does, and it should be. Because In Python < 3.7, the order dictionary key is not guaranteed. mail.python.org/pipermail/python-dev/2017-December/151283.html , docs.python.org/3/whatsnew/3.7.htmlLemuel
@Nepil and if you want it as a sorted dict in python >=3.7, just put dict() around itStlouis
Z
14
>>> from operator import itemgetter
>>> from collections import Counter
>>> c = Counter({'A': 10, 'C':5, 'H':4})
>>> sorted(c.items(), key=itemgetter(0))
[('A', 10), ('C', 5), ('H', 4)]
Zo answered 29/7, 2013 at 17:55 Comment(1)
This works, however, itemgetter is useful to sort a list of tuples or a list of list, but on a dict it is useless, sorted(c) is equivalent to sorted(c.keys())Gavrielle
B
4
sorted(counter.items(),key = lambda i: i[0])

for example:

arr = [2,3,1,3,2,4,6,7,9,2,19]
c = collections.Counter(arr)
sorted(c.items(),key = lambda i: i[0])

outer: [(1, 1), (2, 3), (3, 2), (4, 1), (6, 1), (7, 1), (9, 1), (19, 1)] if you want to get the dictionary format,just

dict(sorted(c.items(),key = lambda i: i[0]))
Berne answered 20/9, 2019 at 8:3 Comment(0)
R
2

To get values as list in sorted order

array              = [1, 2, 3, 4, 5]
counter            = collections.Counter(array)
sorted_occurrences = list(dict(sorted(counter.items())).values())
Regimentals answered 30/1, 2019 at 14:39 Comment(0)
P
0

In Python 3, you can use the most_common function of collections.Counter:

x = ['a', 'b', 'c', 'c', 'c', 'd', 'd']
counts = collections.Counter(x)
counts.most_common(len(counts))

This uses the most_common function available in collections.Counter, which allows you to find the keys and counts of n most common keys.

Palp answered 31/1, 2017 at 21:4 Comment(2)
This doesn't answer the question at all. I get [('c', 3), ('d', 2), ('a', 1), ('b', 1)] which is not sorting the keys alphabetically.Respect
The OP clearly knows about most_common, it's mentioned in the question.Respect
P
0

Refer to my comments below

# input data
counter = Counter({'A': 10, 'C': 5, 'H': 7})
# Sorting the Counter by keys
sorted_counter = sorted(counter.items())
# change it back into a counter
result_counter = Counter(sorted_counter)

the result should look like this:

Counter({'A': 10, 'C': 5, 'H': 7})

i know the input data and the output data is the same, but when u change to "ate" then

{"a" : "1", "t" : "1" , "e" : "1" }

will change to:

{"a" : "1", "e" : "1" , "t" : "1" }
Pancreas answered 25/2 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.