选取样本函数random.sample
正确使用示例
# -*- coding: utf-8 -*- import random lst = [1, 2, 3] ret = random.sample(lst, 2) print(ret) # [3, 2]
如果选取的数量比全量数据多,就会报错
# -*- coding: utf-8 -*- import random lst = [1, 2, 3] ret = random.sample(lst, 4) print(ret) # ValueError: Sample larger than population or is negative