Skip to main content
 首页 » 编程设计

python之函数调用和函数中的代码给出不同的结果

2024年11月01日12davidwang456

<分区>

我的程序中有一个错误,我正在尝试使用带有求值表达式的 Pycharm 调试工具对其进行调试。我发现了我的代码无法正常工作的原因,但不明白这样的事情是如何发生的。当我用一些参数调用我的函数 marginalizedTransition 时,结果是 1.0,但是当我在该函数中运行代码并将参数放在它们的位置时,我得到 0.00101001196095。然而,这些应该是相同的,因为它们是相同的代码。
player = 1,world.amountOfPlayers() = 2 和 world.amountOfPossibleActions = 5。

marginalizedTransitionTest(Ps, [(3, 1), (5, 1)], [(2, 1), (5, 1)], 0, world, 1) 

在哪里

def marginalizedTransitionTest(Ps, sis, sfs, action, world, player): 
    otherPlayers = filter(lambda x: x != player, range(world.amountOfPlayers())) 
    return sum(np.prod([Ps[otherPlayer][tuple(it.chain(*sis))] 
                   [np.insert(actions, player, action)[otherPlayer]] 
                   for otherPlayer in otherPlayers]) 
           for actions in it.product(range(world.amountOfPossibleActions), repeat=world.amountOfPlayers() - 1)) 

给出 4.05514478458 和

sum(np.prod([Ps[otherPlayer][tuple(it.chain(*[(3, 1), (5, 1)]))] 
               [np.insert(actions, 1, 0)[otherPlayer]] 
               for otherPlayer in filter(lambda x: x != 1, range(world.amountOfPlayers()))]) 
       for actions in it.product(range(world.amountOfPossibleActions), repeat=world.amountOfPlayers() - 1)) 

给出 1.0。

我不知道这是否是一个常见问题。如果您需要有关所用函数的更多信息,我可以提供它们,但这篇文章已经变得丑陋了。