質問編集履歴

3

出力結果の追加

2021/02/04 06:29

投稿

yr28
yr28

スコア2

test CHANGED
File without changes
test CHANGED
@@ -73,6 +73,18 @@
73
73
  print('単語の降順リスト')
74
74
 
75
75
  print(d)
76
+
77
+ ```
78
+
79
+ 上記のプログラムの出力結果です。
80
+
81
+ ```Python
82
+
83
+ 単語の個数 28
84
+
85
+ 単語の降順リスト
86
+
87
+ [(12, 'the'), (8, 'in'), (7, 'and'), (7, 'a'), (6, 'to'), (5, 'of'), (5, 'it'), (4, 'tale'), (4, 'one'), (3, 'with'), (3, 's'), (3, 'little'), (3, 'by'), (2, 'while'), (2, 'we'), (2, 'time'), (2, 'thus'), (2, 'three'), (2, 'such'), (2, 'story'), (2, 'our'), (2, 'next'), (2, 'land'), (2, 'is'), (2, 'fancy'), (2, 'd'), (2, 'beneath'), (2, 'are')]
76
88
 
77
89
  ```
78
90
 

2

プログラムの編集

2021/02/04 06:29

投稿

yr28
yr28

スコア2

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
 
32
32
 
33
- 私の作成途中のプログラムです。
33
+ 私の作成途中のプログラム(編集しました)です。
34
34
 
35
35
  ```Python
36
36
 
@@ -41,6 +41,10 @@
41
41
  with open('alice-wonderland.txt', 'r', encoding='utf-8') as f:
42
42
 
43
43
  data = f.read()
44
+
45
+   data=data.lower()
46
+
47
+ data=re.sub(r'[^a-z]', ' ', data)
44
48
 
45
49
  words={}
46
50
 
@@ -58,9 +62,17 @@
58
62
 
59
63
 
60
64
 
61
- for count,word in d:
65
+ l=0
62
66
 
63
- print(word,count)
67
+ for word,count in d:
68
+
69
+ l+=1
70
+
71
+ print('単語の個数',l)
72
+
73
+ print('単語の降順リスト')
74
+
75
+ print(d)
64
76
 
65
77
  ```
66
78
 

1

作成したプログラムを追記いたしました。

2021/02/04 06:28

投稿

yr28
yr28

スコア2

test CHANGED
File without changes
test CHANGED
@@ -27,6 +27,42 @@
27
27
  ('fancy', 2), ('land', 2), ('story', 2), ('next', 2), ('time', 2), ('is', 2),
28
28
 
29
29
  ('thus', 2)]
30
+
31
+
32
+
33
+ 私の作成途中のプログラムです。
34
+
35
+ ```Python
36
+
37
+ import re
38
+
39
+
40
+
41
+ with open('alice-wonderland.txt', 'r', encoding='utf-8') as f:
42
+
43
+ data = f.read()
44
+
45
+ words={}
46
+
47
+ for word in data.split():
48
+
49
+ words[word]=words.get(word,0)+1
50
+
51
+
52
+
53
+ d=[(v,k) for k,v in words.items() if v>=2]
54
+
55
+ d.sort()
56
+
57
+ d.reverse()
58
+
59
+
60
+
61
+ for count,word in d:
62
+
63
+ print(word,count)
64
+
65
+ ```
30
66
 
31
67
 
32
68