質問編集履歴
2
ご回答を受けて試したことの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,6 +116,134 @@
|
|
116
116
|
|
117
117
|
|
118
118
|
|
119
|
+
### ご回答を受けて試したこと
|
120
|
+
|
121
|
+
関数に切り出してみたのですが、うまく切り出すことができず、
|
122
|
+
|
123
|
+
エラーが出てしまいました。関数内でも、alphとtarget_alphを使用する必要があるため、関数を用いてもうまく効率化する術がわかりません。
|
124
|
+
|
125
|
+
```python
|
126
|
+
|
127
|
+
#!/usr/bin/env python
|
128
|
+
|
129
|
+
# coding: utf-8
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
data = {
|
134
|
+
|
135
|
+
'A':[1, 3, 5, 2, 1, 8, 9],
|
136
|
+
|
137
|
+
'B':[9, 4, 3],
|
138
|
+
|
139
|
+
'C':[8, 5, 5, 6, 1]
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
output = {}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
for alph, nums in data.items():
|
150
|
+
|
151
|
+
avg = {}
|
152
|
+
|
153
|
+
my_list = nums
|
154
|
+
|
155
|
+
for target_alph, target_nums in data.items():
|
156
|
+
|
157
|
+
target_list = target_nums
|
158
|
+
|
159
|
+
"""
|
160
|
+
|
161
|
+
if alph == target_alph:
|
162
|
+
|
163
|
+
continue
|
164
|
+
|
165
|
+
max_nums = []
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
for i in my_list:
|
170
|
+
|
171
|
+
max_num = 0
|
172
|
+
|
173
|
+
for j in target_list:
|
174
|
+
|
175
|
+
result = i * j
|
176
|
+
|
177
|
+
if result is not None and result > max_num:
|
178
|
+
|
179
|
+
max_num = result
|
180
|
+
|
181
|
+
max_nums.append(max_num)
|
182
|
+
|
183
|
+
"""
|
184
|
+
|
185
|
+
avg[target_alph] = avg_of_max(my_list, target_list)
|
186
|
+
|
187
|
+
output[alph] = avg
|
188
|
+
|
189
|
+
print(output)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
def avg_of_max_nums(my_list, target_list):
|
196
|
+
|
197
|
+
for alph, nums in data.items():
|
198
|
+
|
199
|
+
for target_alph, target_nums in data.items():
|
200
|
+
|
201
|
+
if alph == target_alph:
|
202
|
+
|
203
|
+
continue
|
204
|
+
|
205
|
+
max_nums = []
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
for i in my_list:
|
210
|
+
|
211
|
+
max_num = 0
|
212
|
+
|
213
|
+
for j in target_list:
|
214
|
+
|
215
|
+
result = i * j
|
216
|
+
|
217
|
+
if result is not None and result > max_num:
|
218
|
+
|
219
|
+
max_num = result
|
220
|
+
|
221
|
+
max_nums.append(max_num)
|
222
|
+
|
223
|
+
return sum(max_nums) / len(max_nums)
|
224
|
+
|
225
|
+
```
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
エラー文
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
$ python sample.py
|
234
|
+
|
235
|
+
File "sample.py", line 49
|
236
|
+
|
237
|
+
return sum(max_nums) / len(max_nums)
|
238
|
+
|
239
|
+
^
|
240
|
+
|
241
|
+
IndentationError: unindent does not match any outer indentation level
|
242
|
+
|
243
|
+
```
|
244
|
+
|
245
|
+
|
246
|
+
|
119
247
|
### 補足情報(FW/ツールのバージョンなど)
|
120
248
|
|
121
249
|
python3.6
|
1
該当のコードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -84,7 +84,7 @@
|
|
84
84
|
|
85
85
|
result = i * j
|
86
86
|
|
87
|
-
if result is not None and result:
|
87
|
+
if result is not None and result > max_num:
|
88
88
|
|
89
89
|
max_num = result
|
90
90
|
|
@@ -104,11 +104,11 @@
|
|
104
104
|
|
105
105
|
{
|
106
106
|
|
107
|
-
'A': {'B':
|
107
|
+
{'A': {'B': 37.285714285714285, 'C': 33.142857142857146},
|
108
108
|
|
109
|
-
|
109
|
+
'B': {'A': 48.0, 'C': 42.666666666666664},
|
110
110
|
|
111
|
-
'C': {'A': 45.0, 'B':
|
111
|
+
'C': {'A': 45.0, 'B': 45.0}}
|
112
112
|
|
113
113
|
}
|
114
114
|
|