回答編集履歴

3

補足を追加

2020/10/10 01:20

投稿

Daregada
Daregada

スコア11990

test CHANGED
@@ -27,3 +27,33 @@
27
27
  print(j + ': ' + str(words.count(j)))
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ 追加分: 標準ライブラリのcollectionに含まれるCounterクラスを使うと、リストの各要素を数えてソートした辞書を作ってくれる。
34
+
35
+
36
+
37
+ ```Python
38
+
39
+ import collections
40
+
41
+
42
+
43
+ words = []
44
+
45
+ sample = " The agency was given the prize for its efforts to combat hunger and improve conditions for peace. The WFP, the 101st winner of a prize now worth 10m Swedish krona ($1.1m; £875,000), said it was 'deeply humbled' to have won. Some 107 organisations and 211 individuals were nominated for the award this year. WFP head David Beasley told the BBC's Newshour he was in shock following the announcement. In the video, Mr Prasad says he and his wife start preparing the menu at 6.30am every morning and finish by 9.30am. The first lot of customers are usually informal workers or office-goers looking for a hearty breakfast. But the pandemic, which cost people their jobs or forced them to stay at home, has shrunk business. Mrs Prasad says that at times they take the unsold food back home, and on some days, they don't have money to cook a meal for themselves. But ever since the video went viral, the stall has seen a steady flow of customers, some of whom are eager to squeeze in a selfie with the now-famous spot. TV crews have been showing up as the stall and its owners continue to grab headlines. Bollywood actress Sonam Kapoor and cricketer R Ashwin are among those who have offered to help. Mr Wasan told ANI that he had come across Baba ka dhaba by chance. 'They told me that were making losses every day. So I made a video at the spot and shared it with my followers.' He adds that he has collected around $2,700 in donations to repair the stall and the couples house. The Prasads are both touched and elated. 'It is all because of Gaurav that the customers have lined up today,' Mr Prasad told ANI news agency. 'Yesterday there was almost no sales. Today I feel that the whole country is with us.' "
46
+
47
+ for i in sample.split():
48
+
49
+ words.append(i.lower())
50
+
51
+
52
+
53
+ counter = collections.Counter(words)
54
+
55
+ for key in counter:
56
+
57
+ print(key + ': ' + str(counter[key]))
58
+
59
+ ```

2

不要なifを削除

2020/10/10 01:20

投稿

Daregada
Daregada

スコア11990

test CHANGED
@@ -18,9 +18,7 @@
18
18
 
19
19
  for i in sample.split():
20
20
 
21
- if type(i) != int:
22
-
23
- words.append(i.lower())
21
+ words.append(i.lower())
24
22
 
25
23
 
26
24
 

1

補足を追加

2020/10/10 01:02

投稿

Daregada
Daregada

スコア11990

test CHANGED
@@ -1,3 +1,13 @@
1
+ - 結果の表示は、最初のforループの中ではなく、繰り返し処理がすべて終わってからにする。つまり、2つめのforループは最初のforループの外側に置く。
2
+
3
+ - エラーの原因は、`words.count(j)`が整数値を返すことにあるので、組み込み関数の`str`で囲んで文字列に変換する
4
+
5
+ - そのさい、英文を格納する変数名が`str`のままだと、同じ名前の組み込み関数`str`が使えなくなってしまう。以下のコードでは、英文を`sample`という別の名前の変数に格納している。
6
+
7
+ - printは、ほっといても末尾で改行するのが既定値なので、`\n`は不要。
8
+
9
+
10
+
1
11
  ```Python
2
12
 
3
13
  words = []