从python的列表中选择一个随机单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4394145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:33:42 来源:igfitidea点击:
Picking a Random Word from a list in python?
提问by Noah R
how in python 3 would I make a list of words then have it print a random word from the list
我如何在 python 3 中制作一个单词列表,然后让它从列表中打印一个随机单词
采纳答案by Greg Hewgill
Use the random.choice()function:
使用random.choice()函数:
>>> import random
>>> a = ["Stack", "Overflow", "rocks"]
>>> print(random.choice(a))
rocks
回答by jtdubs
>>> import random
>>> random.choice("hello world".split())
'hello'
>>> random.choice("hello world".split())
'world'
回答by Ravikiran D
str='book pen paper pencil'
x=str.split()
print(x)
import random
print(random.choice(x))
回答by Ravikiran D
str='book pen paper pencil'
x=str.split()
print(x)
y=len(x)
import random
z=random.randrange(-1,y)
print(x[z])

