2012-07-20 61 views

回答

1

您需要使用随机索引引用您的列表中的项目。

>>> import random 
>>> list=["A","B","C"] 
>>> listitem = random.randint(0,len(list)) 
>>> list[listitem] 
'A' 
>>> listitem = random.randint(0,len(list)) 
>>> list[listitem] 
'B' 

或者,如果你不关心指数,只是随意使用random.choice()函数选择一个项目:

>>> random.choice(list) 
'B' 
>>> random.choice(list) 
'B' 
>>> random.choice(list) 
'A' 
>>> random.choice(list) 
'C' 
+0

感谢布莱斯,这有助于 – 2012-07-20 09:47:45

3


您可以使用random

>>> from random import choice 
>>> List = [ 'A','B','C' ] 
>>> choice(List) 
C 
>>> choice(List) 
A 
>>> choice(List) 
B