Python random selects elements randomly from a collection

USES the python random module’s choice method to randomly select an element

foo = ['a', 'b', 'c', 'd', 'e']
from random import choice
print choice(foo)

USES the python random module’s sample function to randomly select a set of elements from the list

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
slice = random.sample(list, 5)  #从list中随机获取5个元素,作为一个片断返回  
print slice  
print list #原有序列并没有改变。  

Read More: