回答編集履歴
2
追記
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
```Python
|
4
4
|
|
5
|
-
arr = [["A","B","C"],["D","E","F"],["G","H","I"]]
|
5
|
+
arr = [["A", "B", "C"], ["D", "E", "F"], ["G", "H", "I"]]
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -10,9 +10,7 @@
|
|
10
10
|
|
11
11
|
try:
|
12
12
|
|
13
|
-
|
13
|
+
pos = (y, row.index("G"))
|
14
|
-
|
15
|
-
pos = (y, x)
|
16
14
|
|
17
15
|
break
|
18
16
|
|
@@ -20,11 +18,31 @@
|
|
20
18
|
|
21
19
|
pass
|
22
20
|
|
23
|
-
else:
|
24
|
-
|
25
|
-
pos = None
|
26
21
|
|
27
22
|
|
23
|
+
print(pos)
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
ちょっと捻った方法。
|
30
|
+
|
31
|
+
```Python
|
32
|
+
|
33
|
+
from itertools import chain
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
arr = [["A", "B", "C"], ["D", "E", "F"], ["G", "H", "I"]]
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
pos = divmod(
|
42
|
+
|
43
|
+
list(chain(*arr)).index("G"), len(arr[0])
|
44
|
+
|
45
|
+
)
|
28
46
|
|
29
47
|
print(pos)
|
30
48
|
|
1
追記
test
CHANGED
@@ -1,3 +1,57 @@
|
|
1
|
+
素直な方法。
|
2
|
+
|
3
|
+
```Python
|
4
|
+
|
5
|
+
arr = [["A","B","C"],["D","E","F"],["G","H","I"]]
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
for y, row in enumerate(arr):
|
10
|
+
|
11
|
+
try:
|
12
|
+
|
13
|
+
x = row.index("G")
|
14
|
+
|
15
|
+
pos = (y, x)
|
16
|
+
|
17
|
+
break
|
18
|
+
|
19
|
+
except ValueError:
|
20
|
+
|
21
|
+
pass
|
22
|
+
|
23
|
+
else:
|
24
|
+
|
25
|
+
pos = None
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
print(pos)
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
内包表記を使った方法。
|
36
|
+
|
37
|
+
```Python
|
38
|
+
|
39
|
+
arr = [["A", "B", "C"], ["D", "E", "F"], ["G", "H", "I"]]
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
pos = [
|
44
|
+
|
45
|
+
(x, y) for y, row in enumerate(arr) for x, elem in enumerate(row) if arr[x][y] == "G"
|
46
|
+
|
47
|
+
][0]
|
48
|
+
|
49
|
+
print(pos)
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
|
54
|
+
|
1
55
|
NumPyを使うのもアリかもしれませんね。
|
2
56
|
|
3
57
|
```Python
|
@@ -14,6 +68,6 @@
|
|
14
68
|
|
15
69
|
pos = tuple(nd[0] for nd in np.where(arr == "G"))
|
16
70
|
|
17
|
-
print(pos)
|
71
|
+
print(pos)
|
18
72
|
|
19
73
|
```
|