鱼C论坛

 找回密码
 立即注册
查看: 1997|回复: 3

[已解决]sample 这个函数怎么用啊?

[复制链接]
发表于 2021-3-27 23:07:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
sample 这个函数,样本,怎么用啊?在哪可以查到用法?
最佳答案
2021-3-28 20:16:11
本帖最后由 z5560636 于 2021-3-28 20:18 编辑

语法
random.sample(sequence, k)

参数
参数 必需的 描述
sequence 一个序列。 可以是任何序列:列表,集合,范围等。
k 返回列表的大小



               
               
               
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-28 00:25:20 | 显示全部楼层
  1. >>> import random
  2. >>> help(random.sample)
  3. Help on method sample in module random:

  4. sample(population, k) method of random.Random instance
  5.     Chooses k unique random elements from a population sequence or set.
  6.    
  7.     Returns a new list containing elements from the population while
  8.     leaving the original population unchanged.  The resulting list is
  9.     in selection order so that all sub-slices will also be valid random
  10.     samples.  This allows raffle winners (the sample) to be partitioned
  11.     into grand prize and second place winners (the subslices).
  12.    
  13.     Members of the population need not be hashable or unique.  If the
  14.     population contains repeats, then each occurrence is a possible
  15.     selection in the sample.
  16.    
  17.     To choose a sample in a range of integers, use range as an argument.
  18.     This is especially fast and space efficient for sampling from a
  19.     large population:   sample(range(10000000), 60)

  20. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-28 20:16:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 z5560636 于 2021-3-28 20:18 编辑

语法
random.sample(sequence, k)

参数
参数 必需的 描述
sequence 一个序列。 可以是任何序列:列表,集合,范围等。
k 返回列表的大小



               
               
               
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-28 20:49:34 | 显示全部楼层
z5560636 发表于 2021-3-28 20:16
语法
random.sample(sequence, k)

谢谢啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 20:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表