我从这里得到了这个:Generating random words
import random
words = ['hello', 'apple', 'something', 'yeah', 'nope', 'lalala']
''.join(random.sample(words, 10))
applesomethinghellohellolalala
如何对随机单词进行分段,以便获得以下结果?
words = ['apple','something','hello','hello','lala']
答案 0 :(得分:1)
试试这个:
import random
words = ['hello', 'apple', 'something', 'yeah', 'nope', 'lalala']
print [random.sample(words, 1)[0] for i in range(10)]
答案 1 :(得分:0)
而不是使用
random.sample(words, 1)[0]
如同Eugeny提议的那样,我宁愿使用random.choice
:
print [random.choice(words) for i in range(10)]