回答編集履歴

2

コード修正

2020/05/27 07:27

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -38,6 +38,10 @@
38
38
 
39
39
  count += 1
40
40
 
41
+ # もしくはfor文の代わりに count += words.count("Apple")
42
+
43
+
44
+
41
45
 
42
46
 
43
47
  print("Apple:{}回".format(count))

1

コード記載

2020/05/27 07:27

投稿

jeanbiego
jeanbiego

スコア3966

test CHANGED
@@ -3,3 +3,47 @@
3
3
  この部分ですが、lineにAppleが含まれていればcount+1になっています。
4
4
 
5
5
  つまり、その行にAppleがあれば+1、3行あるので+1が3回ということですね。
6
+
7
+
8
+
9
+
10
+
11
+ 以下、参考まで。
12
+
13
+ ```python3
14
+
15
+ import io
16
+
17
+ txt = """Apple Apple Banana Banana Lemon
18
+
19
+ Apple Apple Apple Apple Lemon
20
+
21
+ Apple Apple Apple Apple"""
22
+
23
+
24
+
25
+ f = io.StringIO(txt)
26
+
27
+ count = 0
28
+
29
+
30
+
31
+ for line in f.readlines():
32
+
33
+ words = line.split()
34
+
35
+ for word in words:
36
+
37
+ if word == "Apple":
38
+
39
+ count += 1
40
+
41
+
42
+
43
+ print("Apple:{}回".format(count))
44
+
45
+ # Apple:10回
46
+
47
+ f.close()
48
+
49
+ ```