我可以根据另一个列表或元组的属性稍微修改Python中的列表或元组吗

问题描述 投票:0回答:1

我正在尝试制作一个基于文本的板球游戏,以下是每个球可能产生的结果列表:

possibleBalls = [0, 1 , 2 ,3 ,4, 6, "W"]

然后,我有可能某个击球手将实现与 random.choices 一起使用的每个结果:

batsman1 = [40, 40, 40, 30, 20, 10, 10]

outcomeOver = random.choices(
    possibleBalls, weights=(batsman1), k=6)

(到目前为止有效)

我希望击球手的权重列表乘以投球手的这些权重,以便修改权重:

bowler1 = [0.8, 0.8, 0.8 , 1, 1.2, 1.2, 1.2]

outcomeOver = random.choices(
    possibleBalls, weights=(batsman1 * bowler1), k=6)

但我收到错误:无法将序列乘以“列表”的“非 int”类型

如果我使用元组也一样

python list variables tuples
1个回答
0
投票

如果将

batsman1
转换为 numpy 数组,就可以做到这一点

import numpy as np

batsman1 = np.array([40, 40, 40, 30, 20, 10, 10])
print(batsman1 * bowler1) # [32. 32. 32. 30. 24. 12. 12.]
© www.soinside.com 2019 - 2024. All rights reserved.