回答編集履歴

6

fix comments

2022/12/09 04:05

投稿

ps_aux_grep
ps_aux_grep

スコア1579

test CHANGED
@@ -29,7 +29,7 @@
29
29
 
30
30
  print(result, mx) # ['かめ', 'ごりら', 'まんとひひ', 'ねずみ'] 247631.550049155
31
31
 
32
- # 3行短縮Var.
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

2022/12/08 20:19

投稿

ps_aux_grep
ps_aux_grep

スコア1579

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件より多い場合は更に時間がかかることに注意してください.例えば550件に1割だけ増えたとしたら3771305175件あるので約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

2022/12/08 20:15

投稿

ps_aux_grep
ps_aux_grep

スコア1579

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
- "かめ": [18897, 0.6717759],
16
+ "列1": ['かめ', 'うさぎ', 'ねこ', 'ごりら', 'うぉんばっと', 'まんとひひ', 'ねずみ'],
16
- "うさぎ": [20312, 0.43125207],
17
+ "列2": [18897, 20312, 17557, 24416, 24282, 23890, 23768],
17
- "ねこ": [17557, 0.621294404],
18
+ "列3": [0.6717759, 0.43125207, 0.621294404, 0.659812699, 0.31928901, 0.944748929, 0.445755777],
18
- "ごりら": [24416, 0.659812699],
19
+ }) # pd.read_excel()が成功すれば同じデータになるはず
19
- "うぉんばっと": [24282, 0.31928901],
20
+
20
- "まんとひひ": [23890, 0.944748929],
21
- "ねずみ": [23768, 0.445755777],
21
+ data = df.values
22
- }
23
22
 
24
23
  mx, result = -1, None
25
- for row in combinations(data.items(), 4): # 4つ選ぶ
24
+ for row in combinations(data, 4): # 4つ選ぶ
26
- value = sum([c1 for _, (c1, c2) in row]) * sum([c2 for _, (c1, c2) in row])
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 _, (c1, c2) in row]) * sum([c2 for _, (c1, c2) in row])
33
+ calc = lambda row: sum([c1 for _, c1, _ in row]) * sum([c2 for _, _, c2 in row])
35
- result = max(combinations(data.items(), 4), key = calc)
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

2022/12/08 20:02

投稿

ps_aux_grep
ps_aux_grep

スコア1579

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

2022/12/08 19:59

投稿

ps_aux_grep
ps_aux_grep

スコア1579

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

2022/12/08 19:56

投稿

ps_aux_grep
ps_aux_grep

スコア1579

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