0

Python 2.7で作業しています。

リストを取り、引数リストの値をリスト「チーム」に追加し、特定の位置値を比較して、値に応じて勝ち、負け、または引き分けを返す引数があります。

def starterTrans3(starter):
    wins = 0
    losses = 0
    nd = 0
    team = [[1, 0], [1, 0], [0, 5], [3, -1]]
    random.shuffle(team)
    for t, s in zip(team, starter):
        t.extend(s)
    score_add(team, exit_score(team, starter))
    length = len(starter)
    for i in range(0, length):
        if team[i][4] > 0 and (team[i][1] > -team[i][4]) and team[i][2] >= 5:
            wins += 1
        elif team[i][4] < 0 and (team[i][1] <= -team[i][4]):
            losses += 1
        elif (team[i][4] <= 0 and team[i][1] >= -team[i][4]):
            nd += 1
    return wins, losses, nd

random.shuffle(team) を使用してチーム リストをランダムに並べ替え、結果を何度もシミュレートできるようにしたいと考えています。

私はそれを使用してそれを行うことができます:

def MonteCarlo(starter, x):
    for i in range(0, x):
        print starterTrans3(starter)

しかし、すべてのシミュレーションの勝ち、負け、および引き分けをすべて合計し、シミュレーションの数 (この場合は x) で割り、すべての勝ち、負け、および引き分けの平均を取得できるようにしたいと考えています。シミュレーションの。

starterTrans 関数を変更して、total_wins 変数が += wins に等しいようにしようとしましたが、それを理解できませんでした。何か案は?

4

1 に答える 1

2

私はあなたの主張を理解していないかもしれませんが...

def MonteCarlo(starter, x):
    result = dict(w=0,l=0,n=0)
    for i in range(0, x):
        w,l,n = starterTrans3(starter)
        result['w']+=w
        result['l']+=l
        result['n']+=n
    return result

また

    return result['w']/float(x),result['l']/float(x),result['n']/float(x)
于 2011-11-05T02:18:23.327 に答える