函数选择和选择之间的区别,以及何时使用一个而不是另一个



选择的输出和选择的输出有什么区别?你什么时候使用一个而不是另一个?

test = [1,2,3]
print(random.choices(test))
[1]
print(random.choice(test))
1

choices支持基于weights等其他参数返回多个结果(这就是为什么结果是一个数组(。例如(取自手册(:

choices(['red', 'black', 'green'], [18, 18, 2], k=6)

choice则从列表中返回随机的单个结果。

有关更多信息,请参阅: https://docs.python.org/3/library/random.html

最新更新