Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counter list python 2.7

I have list like this:

Pasang = [0, 4, 4, 5, 1, 7, 6, 7, 5, 7, 4, 9, 0, 10, 1, 10,...., 23, 9, 23, 7, 23]

I count item from that list:

satuan = Counter(pasang)

then I get :

Counter({5: 10, 6: 7, 0: 5, 1: 5, 7: 5, 10: 4, 11: 4, 15: 4,...,14: 1, 21: 1})

I want to get key from counter, so i do this:

satu = satuan.keys()

and I get sorted list like this:

[0, 1, 2, 4, 5,...,21, 22, 23]

but I need an output like this (not sorted):

[5, 6, 0, 1,...,14, 21]

Sorry for my bad english.

like image 316
Majesty Eksa Permana Avatar asked Sep 27 '22 20:09

Majesty Eksa Permana


1 Answers

You probably need:

[key for key, freq in c.most_common()]

where c is the Counter instance.

most_common will return pairs of keys and frequencies, in decreasing order of frequency. Then you extract the key part using a comprehension.

like image 89
JuniorCompressor Avatar answered Oct 07 '22 01:10

JuniorCompressor