python - How to randomly select two elements of a python list?
问题描述
mylist = ['a','b','c','d']
How can I index the list so that I choose only two of the four elements? mylist[]
throws all sorts of errors depending what datatype I put in the square brackets for indexing
解决方案
You can use random.sample
Try this:
import random
mylist = ['a','b','c','d']
print(random.sample(mylist, 2))
推荐阅读
- node.js - 在 Visual Studio 2019 中编译默认 Angular 项目时未找到(但已安装)node.js
- amazon-web-services - NACL 设置到通过 nat 网关连接到 Internet 的私有子网
- r - 根据我的范围规范我的表
- javascript - 我如何从开始时间中减去结束时间而不在 Javascript 计时器中有负分钟
- virtual-machine - 让虚拟机对应用程序来说就像是真实的机器
- machine-learning - 为什么使用 make_friedman1?
- javascript - 无法删除 [object Array] 的属性“1”
- android - 如何在for()循环中将android背景颜色设置为几个线性布局?
- reactjs - Redux-thunk “调度不是函数”
- kotlin - 如何从计时器中获取秒数?(Kotlin)