回答編集履歴
9
変数分離
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
a = {a[0]: a for a in A}
|
30
30
|
|
31
|
-
result = [[*a[b
|
31
|
+
result = [[*a[b0], b1] for b0, b1 in B if b0 in a]
|
32
32
|
|
33
33
|
print(result)
|
34
34
|
|
8
不要処理削除
test
CHANGED
@@ -48,10 +48,6 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
-
C = [b[0] for b in B]
|
52
|
-
|
53
|
-
|
54
|
-
|
55
51
|
result = []
|
56
52
|
|
57
53
|
for b in B:
|
7
辞書版追加
test
CHANGED
@@ -7,6 +7,28 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
result = [[*a, b[1]] for b in B for a in A if a[0] == b[0]]
|
10
|
+
|
11
|
+
print(result)
|
12
|
+
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
データが大量にあるなら、二重ループは時間がかかり過ぎるので、辞書化した方がいいでしょう。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```python
|
22
|
+
|
23
|
+
A = [['apple', 1],['banana', 2],['orange', 3],['grape', 4],['x',0]]
|
24
|
+
|
25
|
+
B = [['apple', 'ringo'],['banana', 'banana'],['orange', 'orenji'],['grape', 'budou']]
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
a = {a[0]: a for a in A}
|
30
|
+
|
31
|
+
result = [[*a[b[0]], b[1]] for b in B if b[0] in a]
|
10
32
|
|
11
33
|
print(result)
|
12
34
|
|
6
コード変更
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
result = [[*a, b[1]] for b in B for a in A if
|
9
|
+
result = [[*a, b[1]] for b in B for a in A if a[0] == b[0]]
|
10
10
|
|
11
11
|
print(result)
|
12
12
|
|
5
コード変更
test
CHANGED
@@ -6,9 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
b = dict(B)
|
10
|
-
|
11
|
-
result = [[
|
9
|
+
result = [[*a, b[1]] for b in B for a in A if b[0] == a[0]]
|
12
10
|
|
13
11
|
print(result)
|
14
12
|
|
4
バグ修正
test
CHANGED
@@ -34,13 +34,13 @@
|
|
34
34
|
|
35
35
|
result = []
|
36
36
|
|
37
|
-
for
|
37
|
+
for b in B:
|
38
38
|
|
39
|
-
|
39
|
+
for a in A:
|
40
40
|
|
41
|
-
|
41
|
+
if a[0] == b[0]:
|
42
42
|
|
43
|
-
|
43
|
+
result.append([*a, b[1]])
|
44
44
|
|
45
45
|
|
46
46
|
|
3
お書きになたコードの修正版を追加
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
```py
|
1
|
+
```python
|
2
2
|
|
3
3
|
A = [['apple', 1],['banana', 2],['orange', 3],['grape', 4]]
|
4
4
|
|
@@ -13,3 +13,37 @@
|
|
13
13
|
print(result)
|
14
14
|
|
15
15
|
```
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
お書きになったコードで対応しました。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
```python
|
24
|
+
|
25
|
+
A = [['apple', 1],['banana', 2],['orange', 3],['grape', 4]]
|
26
|
+
|
27
|
+
B = [['apple', 'ringo'],['banana', 'banana'],['orange', 'orenji'],['grape', 'budou']]
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
C = [b[0] for b in B]
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
result = []
|
36
|
+
|
37
|
+
for c in C:
|
38
|
+
|
39
|
+
for a in A:
|
40
|
+
|
41
|
+
if a[0] == c:
|
42
|
+
|
43
|
+
result.append([*a, c])
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
print(result)
|
48
|
+
|
49
|
+
```
|
2
変数名変更
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
b = dict(B)
|
10
10
|
|
11
|
-
result = [[name,
|
11
|
+
result = [[name, value, b[name]] for name, value in A if name in b]
|
12
12
|
|
13
13
|
print(result)
|
14
14
|
|
1
質問に対応
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
b = dict(B)
|
10
10
|
|
11
|
-
result = [[name, index, b[name]] for name, index in A]
|
11
|
+
result = [[name, index, b[name]] for name, index in A if name in b]
|
12
12
|
|
13
13
|
print(result)
|
14
14
|
|