Skip to content
Advertisement

Python dictionary counter positions change

So i’m trying to count the most repeated values in an text file. By using the Counter method it returns exaclty what im looking for

file.txt

JavaScript

script.py

JavaScript

OUTPUT

JavaScript

But as you can see the output of the final array is not in the same order as the dictionary
(value should be in descending order)

I am expecting an output like

JavaScript

I also tried list(dictn.keys()) but i got the same output :/
Why is the order changing and how can I fix it?

Advertisement

Answer

From the docs, we see that Counter objects: are “unordered collections” – much like dictionaries, (in-fact they are a sub-class). So this means that iterating over .items() won’t give the elements in order of size.

However, we can simply use .most_common which returns a list of tuples – each containing an element and its count. The most important thing being that it is in order.

So all we need to do is use a list-comprehension to extract the first element of each tuple in the list returned. That can be done with:

JavaScript

which gave:

JavaScript

but could also give the following as the counts for '12334' and '00000' are the same. This is unavoidable due to the nature of how dictionaries (and Counters) work. But if this is important, just let me know and I can update the answer.

JavaScript

Note that not all of your code needs to be inside the with statement, once you have created pArray, you can exit the with statement. Also, basic Python uses lists, not arrays!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement