python - 功能选择和选择之间的区别以及何时使用一个而不是另一个
问题描述
选择的输出和选择的输出有什么区别?你什么时候使用一个而不是另一个?
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)
Whilechoice
从列表中返回一个随机的单个结果。
有关更多信息,请参阅: https ://docs.python.org/3/library/random.html
推荐阅读
- xamarin.forms - 如何将 CollectionView 项目居中?
- python - xhtml2pdf pip 安装失败
- ios - SwiftUI - 尽管使用了 @Published 和 @ObservedObject,但视图不会更新
- javascript - 角度路由:如何将所有未知的嵌套路由重定向到一个组件?
- html - 五行汉堡,纯CSS
- java - 是否可以将 Gzip 字节数组转换为字符串?
- php - 显示按类别名称排序的帖子
- c++ - 带有子类的模板实例化和特化顺序
- ios - 为什么它在 viewDidLoad() 中第一次打印出一个空数组而不是附加值的数组?
- node.js - 将图像链接地址编码为 base64 的最简单方法是什么