-1

Working in python 2.7.

I'm sure the code is a little unwieldy, but I'll try to explain it as simply as I can.

I have two lists:

T = [[1,0], [1,0], [0,5]]
S = [[1], [3], [2]]

I need to add the corresponding value from B to the end of the corresponding list in T, so using zip, I put them together.

I then calculate the result of the first value of each list subtracted from the third, and append that value using another zip function.

So when I run my function, the T variable now looks like [[1,0,1,0], [1,0,3,-2], [0,5,2,-2]].

I then have a series of if statements that if certain values are higher or lower than others, the list returns win, loss, or tie.

I would like to simulate the results of my function (starterTrans) multiple times. The problem is that when I use:

def MonteCarlo(T, S, x):
    for i in range(0, x):
        starterTrans(T, S)

For each simulation I am getting a different version of T. So the first time through the simulation T has the appropriate number of elements in each list (four), but after each run through, more and more variables are added.

I need a way to lock T to it's original four variables no matter how many times I want to use it. And I'm struggling finding a way to do so. Any ideas?

I know my code is convoluted, but here it is if it helps anyone follow my attempt to describe my problem:

def starterTrans(team, starter):
    wins = 0
    losses = 0
    nd = 0
    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

def score_add(team, exit_scores):
    for t, e in zip(team, exit_scores):
        t.append(e)
    return team

def exit_score(team, starter):
    exit_scores = []
    length = len(starter)
    for i in range(0, length):
        score = team[i][0]-team[i][3]
        exit_scores.append(score)
    return exit_scores

def MonteCarlo(team, starter, x):
    for i in range(0, x):
        starterTrans(team, starter)

Thanks for any help.

Burton Guster
  • 2,213
  • 8
  • 31
  • 29

2 Answers2

1

I think you just need to change this:

def MonteCarlo(T, S, x):
    for i in range(0, x):
        starterTrans(T, S)

to this:

def MonteCarlo(T, S, x):
    for i in range(0, x):
        starterTrans(T[:], S)

This will pass a copy of T to starterTrans(..) instead of the original list. If you're editing the elements of T in starterTrans(..) this will not help. Here you would need a deep copy. Have a look here for the difference between shallow and deep copies: What is the difference between a deep copy and a shallow copy?.

Community
  • 1
  • 1
shookster
  • 1,671
  • 1
  • 12
  • 10
0

Change the last line to starterTrans(team[:], starter). That will pass in a copy of team, leaving the original intact.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485