美文网首页测试开发
Python_从list中随机选择

Python_从list中随机选择

作者: 古佛青灯度流年 | 来源:发表于2017-08-30 14:12 被阅读230次

使用Python random模块的choice方法随机选择某个元素

foo = ['a', 'b', 'c', 'd', 'e']
from random import choice
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 #原有序列并没有改变。

相关文章

网友评论

    本文标题:Python_从list中随机选择

    本文链接:https://www.haomeiwen.com/subject/xostjxtx.html