首页 > 解决方案 > 功能选择和选择之间的区别以及何时使用一个而不是另一个

问题描述

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

test = [1,2,3]

print(random.choices(test))

[1]

print(random.choice(test))

1

标签: python

解决方案


choices支持基于附加参数(如weights. 例如(取自手册):

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

Whilechoice从列表中返回一个随机的单个结果。

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


推荐阅读