序列中的随机字符串

use*_*223 1 python random

有没有办法随机化一组字符串,就像你可以为整数?

我举个例子:

import random
random.randint(1, 30) #will produce random number between 1 and 30
Run Code Online (Sandbox Code Playgroud)

对于一个字符串,我想从一个set变量中随机化单词:

a="off","it","on","not"
random.randstr(a) #I understand that this isnt a real code and will produce and error
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法可以实现这一目标?

Ósc*_*pez 16

试试这个,这是惯用的解决方案:

import random
a = ['off', 'it', 'on', 'not']
random.choice(a)
=> 'on' # just an example, it's a random output
Run Code Online (Sandbox Code Playgroud)

上面的代码使用了该choice()函数,请查看文档以获取更多详细信息.