回答編集履歴
6
fix comments
test
CHANGED
@@ -29,7 +29,7 @@
|
|
29
29
|
|
30
30
|
print(result, mx) # ['かめ', 'ごりら', 'まんとひひ', 'ねずみ'] 247631.550049155
|
31
31
|
|
32
|
-
# 3行短縮V
|
32
|
+
# 3行短縮Ver.
|
33
33
|
calc = lambda row: sum([c1 for _, c1, _ in row]) * sum([c2 for _, _, c2 in row])
|
34
34
|
result = max(combinations(data, 4), key = calc)
|
35
35
|
print(f"{', '.join([name for name, _, _ in result])}: {calc(result)}") # かめ, ごりら, まんとひひ, ねずみ: 247631.550049155
|
5
fix answer
test
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
の方を解決すべくコードを例示します.
|
6
6
|
> もとのデータは500行超あるため、並べあげるだけでスプレッドシートで扱える18278行を超過
|
7
7
|
仮にデータが500件だと仮定しても4つ選ぶ組み合わせ(Combinations)は
|
8
|
-
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.500件より多い場合は更に時間がかかることに注意してください.例えば5
|
8
|
+
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.500件より多い場合は更に時間がかかることに注意してください.例えば現状の500件から600件に2割だけ増えたとしても5346164850組あるので約10割もの処理時間増加になります.
|
9
9
|
組み合わせ列挙は[Python標準のモジュール`itertools`](https://docs.python.org/ja/3/library/itertools.html#itertools.combinations)を利用して解決します.
|
10
10
|
|
11
11
|
```Python
|
4
fix answer
test
CHANGED
@@ -9,30 +9,29 @@
|
|
9
9
|
組み合わせ列挙は[Python標準のモジュール`itertools`](https://docs.python.org/ja/3/library/itertools.html#itertools.combinations)を利用して解決します.
|
10
10
|
|
11
11
|
```Python
|
12
|
+
import pandas as pd
|
12
13
|
from itertools import combinations
|
13
14
|
|
14
|
-
data
|
15
|
+
df = pd.DataFrame({
|
15
|
-
"
|
16
|
+
"列1": ['かめ', 'うさぎ', 'ねこ', 'ごりら', 'うぉんばっと', 'まんとひひ', 'ねずみ'],
|
16
|
-
"
|
17
|
+
"列2": [18897, 20312, 17557, 24416, 24282, 23890, 23768],
|
17
|
-
"
|
18
|
+
"列3": [0.6717759, 0.43125207, 0.621294404, 0.659812699, 0.31928901, 0.944748929, 0.445755777],
|
18
|
-
|
19
|
+
}) # pd.read_excel()が成功すれば同じデータになるはず
|
19
|
-
|
20
|
+
|
20
|
-
"まんとひひ": [23890, 0.944748929],
|
21
|
-
|
21
|
+
data = df.values
|
22
|
-
}
|
23
22
|
|
24
23
|
mx, result = -1, None
|
25
|
-
for row in combinations(data
|
24
|
+
for row in combinations(data, 4): # 4つ選ぶ
|
26
|
-
value = sum([c1 for _,
|
25
|
+
value = sum([c1 for _, c1, _ in row]) * sum([c2 for _, _, c2 in row])
|
27
26
|
if mx < value:
|
28
27
|
mx = value
|
29
|
-
result = [name for name, _ in row]
|
28
|
+
result = [name for name, _, _ in row]
|
30
29
|
|
31
30
|
print(result, mx) # ['かめ', 'ごりら', 'まんとひひ', 'ねずみ'] 247631.550049155
|
32
31
|
|
33
32
|
# 3行短縮Var.
|
34
|
-
calc = lambda row: sum([c1 for _,
|
33
|
+
calc = lambda row: sum([c1 for _, c1, _ in row]) * sum([c2 for _, _, c2 in row])
|
35
|
-
result = max(combinations(data
|
34
|
+
result = max(combinations(data, 4), key = calc)
|
36
|
-
print(f"{', '.join([name for name, _ in result])}: {calc(result)}") # かめ, ごりら, まんとひひ, ねずみ: 247631.550049155
|
35
|
+
print(f"{', '.join([name for name, _, _ in result])}: {calc(result)}") # かめ, ごりら, まんとひひ, ねずみ: 247631.550049155
|
37
36
|
```
|
38
37
|
エクセルファイルを適切に読み込めて,例示コードの`data`と同等のものが得られたら,欲しい組み合わせも得られるでしょう.
|
3
fix answer
test
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
の方を解決すべくコードを例示します.
|
6
6
|
> もとのデータは500行超あるため、並べあげるだけでスプレッドシートで扱える18278行を超過
|
7
7
|
仮にデータが500件だと仮定しても4つ選ぶ組み合わせ(Combinations)は
|
8
|
-
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.500件より多い場合は
|
8
|
+
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.500件より多い場合は更に時間がかかることに注意してください.例えば550件に1割だけ増えたとしたら3771305175件あるので約5割の処理時間増加になります.
|
9
9
|
組み合わせ列挙は[Python標準のモジュール`itertools`](https://docs.python.org/ja/3/library/itertools.html#itertools.combinations)を利用して解決します.
|
10
10
|
|
11
11
|
```Python
|
2
fix answer
test
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
の方を解決すべくコードを例示します.
|
6
6
|
> もとのデータは500行超あるため、並べあげるだけでスプレッドシートで扱える18278行を超過
|
7
7
|
仮にデータが500件だと仮定しても4つ選ぶ組み合わせ(Combinations)は
|
8
|
-
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.
|
8
|
+
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.500件より多い場合は列挙に時間がかかることに注意してください.
|
9
9
|
組み合わせ列挙は[Python標準のモジュール`itertools`](https://docs.python.org/ja/3/library/itertools.html#itertools.combinations)を利用して解決します.
|
10
10
|
|
11
11
|
```Python
|
1
update answer
test
CHANGED
@@ -4,8 +4,9 @@
|
|
4
4
|
> この作業がPythonでできるのか疑問になってきました。
|
5
5
|
の方を解決すべくコードを例示します.
|
6
6
|
> もとのデータは500行超あるため、並べあげるだけでスプレッドシートで扱える18278行を超過
|
7
|
-
仮にデータが500件だと仮定しても4つ選ぶ組み合わせは
|
7
|
+
仮にデータが500件だと仮定しても4つ選ぶ組み合わせ(Combinations)は
|
8
8
|
`nCr = 500C4 = 500! / (4! x 496!) = 2573031125`通りです.
|
9
|
+
組み合わせ列挙は[Python標準のモジュール`itertools`](https://docs.python.org/ja/3/library/itertools.html#itertools.combinations)を利用して解決します.
|
9
10
|
|
10
11
|
```Python
|
11
12
|
from itertools import combinations
|