#author("2020-06-12T08:18:08+00:00","default:f-lab","f-lab") #author("2020-06-12T08:33:11+00:00","default:f-lab","f-lab") [[seminar-personal/tei2020]]&br; [[N班ゼミ>http://f-lab.mydns.jp/index.php?seminar-N-2020]] |~目次| |#contents| &br; *英語のテキストを読んで、単語数をカウントするプログラムを作成する. [#x2ed0ef3] # open the file f = open('englishpassage.txt') data = f.read() # build a dictionary words = {} # count values of each key for word in data.split(): words[word] = words.get(word,0)+1 # reverse key and value d = [(v,k) for k,v in words.items()] # sort by count in descending number d.sort(reverse=True) d.sort(reverse=True) # or d = sorted(d,reverse=True) # get top100 in dic for count, word in d[:100]: print(count,word) # 以下は出力 2480 the 1661 I 1427 to 1254 and 1237 of 1175 a 732 was 720 in 495 it 459 that 431 my 414 with 398 he 397 not 396 is 366 as 347 for 335 be 333 but 297 at 293 on 285 or 284 you 281 me 281 had 255 would 245 The 243 his 231 this 226 have 224 by 193 one 190 so 186 if 177 they 177 Red 171 from 166 all 160 about 145 were 139 no 137 him 136 there 134 like 134 are 122 out 118 when 116 which 115 Shirt 113 up 113 do 111 we 111 them 111 If 108 been 106 could 103 such 103 Porcupine 101 did 99 said 97 she 97 He 96 who 96 get 96 an 95 any 93 what 92 It 91 may 90 will 88 some 86 should 85 more 80 their 78 old 77 Project 76 than 75 only 75 her 73 going 73 go 71 make 69 two 69 But 68 school 67 house 67 asked 66 teacher 66 other 66 am 63 three 63 might 63 made 62 thought 62 me. 62 come 61 your 60 room 60 into 59 told