python random 从集合中随机选择元素
使用python random模块的choice方法随机选择某个元素
from random import choicefoo = ['a', 'b', 'c', 'd', 'e']print (choice(foo))
使用python random模块的sample函数从列表中随机选择一组元素
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回 print (slice) print list #原有序列并没有改变。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。