pythonに関する質問です!# 出力 ----------print(pulp.LpStatus[status])print(f\u0026quot;total: {total_buy.value()}\u0026quot;)for horse in horses: print(f\u0026quot;{horse}: {buy[horse].value()} ({(unit_cost * buy[horse] * odds[horse] - total_buy).value()})\u0026quot;)出力の箇所、for文を回した結果をプリントしたものOptimal total: 8600.0[2, 8, 14]: 26.0 (51460.0)[2, 8, 15]: 3.0 (58330.0)[2, 8, 11]: 5.0 (52450.0)[2, 8, 9]: 3.0 (66460.0)[2, 6, 8]: 13.0 (50810.0)[2, 8, 12]: 25.0 (51150.0)[2, 7, 8]: 11.0 (54650.0)↑こちらをデータフレームや辞書にに格納するコードをご教授お願い致します。

1件の回答

回答を書く

1025426

2026-03-08 06:00

+ フォロー

Pythonでfor文の結果をデータフレームや辞書に格納する方法を以下に示します。

■辞書に格納する方法
python
results = {}
for horse in horses:
profit = (unit_cost * buy[horse] * odds[horse] - total_buy).value()
results[str(horse)] = {
'buy_amount': buy[horse].value(),
'profit': profit
}


■データフレームに格納する方法
python
import pandas as pd

data = []
for horse in horses:
profit = (unit_cost * buy[horse] * odds[horse] - total_buy).value()
data.append({
'horse': str(horse),
'buy_amount': buy[horse].value(),
'profit': profit
})

df = pd.DataFrame(data)


■リスト内包表記を使った簡潔な方法
python
import pandas as pd

df = pd.DataFrame([
{
'horse': str(horse),
'buy_amount': buy[horse].value(),
'profit': (unit_cost * buy[horse] * odds[horse] - total_buy).value()
}
for horse in horses
])


データフレームの場合、
df.to_csv('result.csv')で保存したり、print(df)で表形式で表示できます。

うったえる有益だ(0シェアするブックマークする

関連質問

Copyright © 2026 AQ188.com All Rights Reserved.

博識 著作権所有