theteddyboy
theteddyboy

Reputation: 386

How can I sort data in python by the specific part of data?

I have data in my dictionary and I print it out by using the command

for i in mycount.items():
    print i 

The result is

('cooking', Counter({'VG': 26, 'N': 6}))
('Manager', Counter({'N': 1}))
('Hamilton', Counter({'NP': 17}))
('designing', Counter({'VG': 8, 'N': 1}))
('Niagara', Counter({'NP': 2}))
('hallucinating', Counter({'VG': 1}))
('succumb', Counter({'V': 1}))
('shocks', Counter({'N': 4, 'VBZ': 1}))
('crouch', Counter({'N': 2, 'V': 2}))

I want to have the final output as a list of words which is order by the highest number to the lowest number:

cooking   VG_26 N_6
Hamiliton NP_17
designing VG_8

My question is how can i sort the data by the specific part of the data? I am using the itemgetter but the result is as below: (it is a partial result of the whole data)

for x in sorted(mycount.items(), key=itemgetter(1), reverse=True)[0:50]:
    print json.dumps(x)

["best", {"ADV": 51, "DET": 3, "ADJ": 286, "V": 1}]
["open", {"ADV": 8, "N": 4, "ADJ": 238, "V": 55}]
["better", {"ADV": 159, "DET": 4, "ADJ": 233, "V": 6}]
["right", {"ADV": 75, "DET": 121, "ADJ": 222, "N": 179}]

Note: I will clean the data to be the required format once i finish the sorting. Please guide me. Thank you in advanced.

Upvotes: 0

Views: 134

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122252

You probably want:

sorted(mycount.items(), key=lambda kv: [c[1] for c in kv[1].most_common()], revers=True)

e.g. sort by counts contained in the counter, highest to lowest.

By using Counter().most_common() we get the keys and values of the Counter in sorted order, letting us pick out the frequencies as a sort key; because we take all frequencies into account Niagara is sorted after crouch because the latter has more frequencies listed.

Demo:

>>> from pprint import pprint
>>> pprint(sorted(mycount.items(), key=lambda kv: [c[1] for c in kv[1].most_common()], reverse=True))
[('cooking', Counter({'VG': 26, 'N': 6})),
 ('Hamilton', Counter({'NP': 17})),
 ('designing', Counter({'VG': 8, 'N': 1})),
 ('shocks', Counter({'N': 4, 'VBZ': 1})),
 ('crouch', Counter({'N': 2, 'V': 2})),
 ('Niagara', Counter({'NP': 2})),
 ('Manager', Counter({'N': 1})),
 ('hallucinating', Counter({'VG': 1})),
 ('succumb', Counter({'V': 1}))]

Upvotes: 2

Related Questions