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

回答編集履歴

3

追記

2018/08/21 03:56

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -21,4 +21,26 @@
21
21
  リスト内包表記という記法もあります。
22
22
  ```Python
23
23
  list_C = [a+b+'\n' for a, b in zip(list_A, list_B)]
24
+ ```
25
+
26
+ 質問編集を受けて
27
+ ---
28
+ > A.txt内のデータに該当する文字があれば1,なければ0をlistに格納し、B.txt内のデータをjaに格納、jaとlistの各要素を結合しres[]に格納したいです。
29
+
30
+ 文字列と数値を結合したいということでしょうか?
31
+ それならば、少し工夫が必要です。
32
+ ```Python
33
+ >>> a = 'spam'
34
+ >>> b = 42
35
+ >>>
36
+ >>> a + b
37
+ Traceback (most recent call last):
38
+ File "<stdin>", line 1, in <module>
39
+ TypeError: must be str, not int
40
+ >>>
41
+ >>> a + str(b)
42
+ 'spam42'
43
+ >>>
44
+ >>> f'{a}{b}'
45
+ 'spam42'
24
46
  ```

2

追記

2018/08/21 03:56

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -6,6 +6,10 @@
6
6
  不要なイコールが多過ぎます。
7
7
  その方針で書くなら`list_C[0] = list_A[0] + list_B[0]`です。
8
8
 
9
+ **追記:**
10
+ あれ、これならTypeErrorではなくSyntaxErrorが出るはずですね。
11
+ 貼るコードを間違えて居ませんか?
12
+
9
13
  ---
10
14
  リスト長に関係なく書くなら、[zip](https://docs.python.jp/3/library/functions.html#zip)が便利です。
11
15
  ```Python

1

リンクの追加

2018/08/21 02:31

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -7,7 +7,7 @@
7
7
  その方針で書くなら`list_C[0] = list_A[0] + list_B[0]`です。
8
8
 
9
9
  ---
10
- リスト長に関係なく書くなら、zipが便利です。
10
+ リスト長に関係なく書くなら、[zip](https://docs.python.jp/3/library/functions.html#zip)が便利です。
11
11
  ```Python
12
12
  list_C = []
13
13
  for a, b in zip(list_A, list_B):