Python:sample函数

sample(序列a,n)

功能:从序列a中随机抽取n个元素,并将n个元素生以list形式返回。

例:

  • from random import randint, sample
    date = [randint(10,20) for _ in range(10)]
    c = sample(date, 5)
    print(c)
    # 输出:[12, 17, 10, 12, 17]
  1. randint(10,20) for _ in range(10):从10~20间随机抽取10个数;
posted @ 2018-04-06 14:33  何永灿  阅读(65012)  评论(1编辑  收藏  举报