开发者

Picking a Random Word from a list in python?

开发者 https://www.devze.com 2023-01-29 08:28 出处:网络
In Python 3, how would开发者_如何学Go I print a random word from a list of words?Use the random.choice() function:

In Python 3, how would开发者_如何学Go I print a random word from a list of words?


Use the random.choice() function:

>>> import random
>>> a = ["Stack", "Overflow", "rocks"]
>>> print(random.choice(a))
rocks


>>> import random
>>> random.choice("hello world".split())
'hello'
>>> random.choice("hello world".split())
'world'


str='book pen paper pencil'
x=str.split()
print(x)
import random
print(random.choice(x))


str='book pen paper pencil'
x=str.split()
print(x)
y=len(x)
import random
z=random.randrange(-1,y)
print(x[z])
0

精彩评论

暂无评论...
验证码 换一张
取 消