[英]Python random.choices() query
import random
a_list = [1,2]
dist = [0.9,0.1]
random_number = random.choices(a_list,dist)
print(random_number)
Is it possible to get the output of this as an int?是否可以将其输出作为int? I am building a game of cricket where I need the random choice to be added the the players current score but cannot as Py cannot cannot perform additions on list and get TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
.我正在构建一个板球游戏,我需要随机选择来添加玩家当前得分,但不能,因为 Py 无法在列表上执行添加并获取TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
。
When called without a k
argument, random.choices()
will return a list containing one element.当不带k
参数调用时, random.choices()
将返回一个包含一个元素的列表。 You can then withdraw that one element like you'd access any other list element.然后,您可以像访问任何其他列表元素一样撤回该元素。
random_number = random.choice(a_list, dist)[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.