回答編集履歴
3
追記
test
CHANGED
@@ -79,3 +79,57 @@
|
|
79
79
|
print(argmax_2dim(a))
|
80
80
|
|
81
81
|
```
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
皆さん『複数最大値がある場合』について言及されているので、便乗してみる。
|
86
|
+
|
87
|
+
```Python
|
88
|
+
|
89
|
+
def flatten_2dim(array):
|
90
|
+
|
91
|
+
return [item for sublist in array for item in sublist]
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
def all_argmax_2dim(array):
|
96
|
+
|
97
|
+
flat_array = flatten_2dim(array)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
row_num = len(array)
|
102
|
+
|
103
|
+
max_value = max(flat_array)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
for i, elem in enumerate(flat_array):
|
108
|
+
|
109
|
+
if elem == max_value:
|
110
|
+
|
111
|
+
yield i // row_num, i % row_num
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
a = [[0,5,5],[2,2,5],[5,1,3]]
|
116
|
+
|
117
|
+
for argmax in all_argmax_2dim(a):
|
118
|
+
|
119
|
+
print(argmax)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
"""出力
|
124
|
+
|
125
|
+
(0, 1)
|
126
|
+
|
127
|
+
(0, 2)
|
128
|
+
|
129
|
+
(1, 2)
|
130
|
+
|
131
|
+
(2, 0)
|
132
|
+
|
133
|
+
"""
|
134
|
+
|
135
|
+
```
|
2
こっそりバグ修正
test
CHANGED
@@ -66,7 +66,7 @@
|
|
66
66
|
|
67
67
|
row_num = len(array)
|
68
68
|
|
69
|
-
max_index = max(flat_array)
|
69
|
+
max_index = flat_array.index(max(flat_array))
|
70
70
|
|
71
71
|
|
72
72
|
|
1
追記
test
CHANGED
@@ -43,3 +43,39 @@
|
|
43
43
|
|
44
44
|
|
45
45
|
ラムダ式がわかるならそのまま使った方が簡潔でしょう。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
---
|
50
|
+
|
51
|
+
どうしてもnumpyを使いたくない場合。
|
52
|
+
|
53
|
+
```Python
|
54
|
+
|
55
|
+
def flatten_2dim(array):
|
56
|
+
|
57
|
+
return [item for sublist in array for item in sublist]
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
def argmax_2dim(array):
|
62
|
+
|
63
|
+
flat_array = flatten_2dim(array)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
row_num = len(array)
|
68
|
+
|
69
|
+
max_index = max(flat_array)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
return max_index // row_num, max_index % row_num
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
a = [[0,1,1],[2,2,5],[2,1,3]]
|
78
|
+
|
79
|
+
print(argmax_2dim(a))
|
80
|
+
|
81
|
+
```
|