teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コードを整形

2019/05/16 04:10

投稿

Kentaro0919
Kentaro0919

スコア258

answer CHANGED
@@ -1,4 +1,37 @@
1
1
  マークダウンにコードを埋め込む方法があるので 利用してはいかがでしょうか
2
2
 
3
+ ```pyhon
4
+ import re
5
+
6
+ f_test = open('hoge.txt','r') #txtをインポート。ちゃんとできたかprintで確認。
7
+ lines = f_test.readlines()
8
+ for line in lines:
9
+ print(line)
10
+
11
+ f_test.close()
12
+
13
+ with open('hoge.txt','r') as f: #全て小文字に変換。ちゃんとできたかprintで確認。
14
+ lines=f.read().lower()
15
+ print(lines)
16
+
17
+ def words_list(lines): #スペースで区切って単語リストを作る。printで確認できない。
18
+ return re.split(("[^a-z]+",lines)[:-1])
19
+
20
+ print(word_list)
21
+
22
+ def words_to_count(word_list): #辞書を作って単語をカウント。printで確認できない。
23
+ wdict={word:0 for word in set(word_list)}
24
+ for w in word_list:
25
+ wdict[w] += 1
26
+ return wdict
27
+
28
+ print(wdict)
29
+
30
+ def print_words(word_dict): #降順に並び替え。printで確認できない。
31
+ for word in sorted(word_dict,key=lambda x:(-word_dict[x],x)):
32
+
33
+ print(word,word_dict[word])
34
+ ```
35
+
3
36
  コード修正の参考に
4
37
  https://qiita.com/hatchinee/items/a904c1f8d732a4686c9d