言語処理100本ノック

19. 各行の1コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べる

各行の1列目の文字列の出現頻度を求め,その高い順に並べて表示せよ.確認にはcut, uniq, sortコマンドを用いよ.

from collections import Counter

with open("D:\\nlp100\\hightemp.txt",encoding="UTF-8") as fr:
    list = fr.readlines()

list_split = [i.split() for i in list]

cnt = Counter()
for prefecture in list_split:
    cnt[prefecture[0]] += 1

sort_list = []
for pref in cnt.most_common():  
    for ls in list_split:
        if pref[0] == ls[0]:
            sort_list.append(ls)
            break

print(sort_list)
<結果>
[['山形県', '山形', '40.8', '1933-07-25'], ['山梨県', '甲府', '40.7', '2013-08-10'], ['群馬県', '館林', '40.3', '2007-08-16'], ['埼玉県', '熊谷', '40.9', '2007-08-16'], ['静岡県', '天竜', '40.6', '1994-08-04'], ['愛知県', '愛西', '40.3', '1994-08-05'], ['千葉県', '牛久', '40.2', '2004-07-20'], ['岐阜県', '多治見', '40.9', '2007-08-16'], ['大阪府', '豊中', '39.9', '1994-08-08'], ['和歌山県', 'かつらぎ', '40.6', '1994-08-08'], ['愛媛県', '宇和島', '40.2', '1927-07-22'], ['高知県', '江川崎', '41', '2013-08-12']]