回答編集履歴
2
コード修正
answer
CHANGED
@@ -18,7 +18,9 @@
|
|
18
18
|
for word in words:
|
19
19
|
if word == "Apple":
|
20
20
|
count += 1
|
21
|
+
# もしくはfor文の代わりに count += words.count("Apple")
|
21
22
|
|
23
|
+
|
22
24
|
print("Apple:{}回".format(count))
|
23
25
|
# Apple:10回
|
24
26
|
f.close()
|
1
コード記載
answer
CHANGED
@@ -1,3 +1,25 @@
|
|
1
1
|
if "Apple" in line:
|
2
2
|
この部分ですが、lineにAppleが含まれていればcount+1になっています。
|
3
|
-
つまり、その行にAppleがあれば+1、3行あるので+1が3回ということですね。
|
3
|
+
つまり、その行にAppleがあれば+1、3行あるので+1が3回ということですね。
|
4
|
+
|
5
|
+
|
6
|
+
以下、参考まで。
|
7
|
+
```python3
|
8
|
+
import io
|
9
|
+
txt = """Apple Apple Banana Banana Lemon
|
10
|
+
Apple Apple Apple Apple Lemon
|
11
|
+
Apple Apple Apple Apple"""
|
12
|
+
|
13
|
+
f = io.StringIO(txt)
|
14
|
+
count = 0
|
15
|
+
|
16
|
+
for line in f.readlines():
|
17
|
+
words = line.split()
|
18
|
+
for word in words:
|
19
|
+
if word == "Apple":
|
20
|
+
count += 1
|
21
|
+
|
22
|
+
print("Apple:{}回".format(count))
|
23
|
+
# Apple:10回
|
24
|
+
f.close()
|
25
|
+
```
|