如何从Python中的数组中选择一个随机元素?(复制)

时间:2022-10-30 14:35:30

This question already has an answer here:

这个问题已经有了答案:

The first examples that I googled didn't work. This should be trivial, right?

我用谷歌搜索的第一个例子是无效的。这应该很简单,对吧?

2 个解决方案

#1


133  

import random
random.choice (mylist)

#2


27  

import random
random.choice([1, 2, 3])

#1


133  

import random
random.choice (mylist)

#2


27  

import random
random.choice([1, 2, 3])