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