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个数;

转载于:https://www.cnblogs.com/volcao/p/8727688.html

你可能感兴趣的:(Python:sample函数)