运行 for 循环 monte carlo sim 时出现 zip 问题; Python
在 python 2.7 中工作。
我确信代码有点笨拙,但我会尽力尽可能简单地解释它。
我有两个列表:
T = [[1,0], [1,0], [0,5]]
S = [[1], [3], [2]]
我需要将 B 中的相应值添加到 T 中相应列表的末尾,因此使用 zip,我将它们放在一起。
然后,我计算每个列表的第一个值减去第三个值的结果,并使用另一个 zip 函数附加该值。
因此,当我运行函数时,T 变量现在看起来像 [[1,0,1,0], [1,0,3,-2], [0,5,2,-2]]。
然后,我有一系列 if 语句,如果某些值高于或低于其他值,则列表返回赢、输或平局。
我想多次模拟我的函数(starterTrans)的结果。问题是,当我使用:
def MonteCarlo(T, S, x):
for i in range(0, x):
starterTrans(T, S)
对于每次模拟,我都会得到 T 的不同版本。因此,第一次通过模拟 T 在每个列表中具有适当数量的元素(四个),但每次运行后,越来越多的元素添加了变量。
我需要一种方法将 T 锁定为其原始的四个变量,无论我想使用它多少次。我正在努力寻找一种方法来做到这一点。有什么想法吗?
我知道我的代码很复杂,但如果它可以帮助任何人遵循我描述我的问题的尝试:
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)
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您只需要将以下内容更改
为:
这会将
T
的副本传递给starterTrans(..)
而不是原始列表。如果您正在starterTrans(..)
中编辑T
的元素,这将无济于事。在这里你需要一个深拷贝。在这里看看浅拷贝和深拷贝之间的区别:深复制和浅复制有什么区别?。I think you just need to change this:
to this:
This will pass a copy of
T
tostarterTrans(..)
instead of the original list. If you're editing the elements ofT
instarterTrans(..)
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?.将最后一行更改为
starterTrans(team[:], starter)
。这将传递 team 的副本,而原始版本保持不变。Change the last line to
starterTrans(team[:], starter)
. That will pass in a copy of team, leaving the original intact.