python random函数sample_Python random.sample()用法及代码示例
sample()是Python中随机模块的内置函数,可返回从序列中选择的项目的特定长度列表,即列表,元组,字符串或集合。用于随机抽样而无需更换。
用法: random.sample(sequence, k)
参数:
sequence:可以是列表,元组,字符串或集合。
k:一个整数值,它指定样本的长度。
返回:k长度从序列中选择的新元素列表。
代码1:sample()函数的简单实现。
# Python3 program to demonstrate
# the use of sample() function .
# import random
from random import sample
# Prints list of random items of given length
list1 = [1, 2, 3, 4, 5]
print(sample(list1,3))
输出:
[2, 3, 5]
代码2:sample()函数的基本用法。
# Python3 program to demonstrate
# the use of sample() function .
# import random
import random
# Prints list of random items of
# length 3 from the given list.
list1 = [1, 2, 3, 4, 5, 6]
print("With list:", random.sample(list1, 3))
# Prints list of random items of
# length 4 from the given string.
string = "GeeksforGeeks"
print("With string:", random.sample(string, 4))
# Prints list of random items of
# length 4 from the given tuple.
tuple1 = ("ankit", "geeks", "computer", "science",
"portal", "scientist", "btech")
print("With tuple:", random.sample(tuple1, 4))
# Prints list of random items of
# length 3 from the given set.
set1 = {"a", "b", "c", "d", "e"}
print("With set:", random.sample(set1, 3))
输出:
With list:[3, 1, 2]
With string:['e', 'f', 'G', 'G']
With tuple:['ankit', 'portal', 'geeks', 'computer']
With set:['b', 'd', 'c']
注意:每次返回随机项目时,输出都会有所不同。
代码3:引发异常
如果样本大小(即k)大于序列大小,则会引发ValueError。
# Python3 program to demonstrate the
# error of sample() function.
import random
list1 = [1, 2, 3, 4]
# exception raised
print(random.sample(list1, 5))
输出:
Traceback (most recent call last):
File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in
print(random.sample(list1, 5))
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample
raise ValueError("Sample larger than population or is negative")
ValueError:Sample larger than population or is negative
python random函数sample_Python random.sample()用法及代码示例相关推荐
- python cos函数_Python Tensorflow cos()用法及代码示例
Tensorflow是Google开发的开源机器学习库.它的应用之一是开发深度神经网络. 模块tensorflow.math为许多基本的数学运算提供支持.功能tf.cos()[别名tf.math.co ...
- python random函数sample_Python random.seed() random.sample()函数使用
random.seed(0)作用:使得随机数据可预测,即只要seed的值一样,后续生成的随机数都一样. 一.不设置seed() import random list = [1, 2, 3, 4, 5, ...
- python中argmin函数_Python numpy.argmin()用法及代码示例
numpy.argmin(array,axis = None,out = None):返回特定轴上数组min元素的索引. 参数: array:Input array to work on axis : ...
- python not is函数_Python unittest assertIsNotNone()用法及代码示例
assertIsNotNonePython中的()是单元测试库函数,用于单元测试中以检查输入值是否为None.此函数将使用两个参数作为输入,并根据断言条件返回布尔值.如果输入值不等于无assertIs ...
- python中squeeze函数_Python numpy.squeeze()用法及代码示例
当我们要从数组形状中删除一维条目时,将使用numpy.squeeze()函数. 用法: numpy.squeeze(arr, axis=None ) 参数: arr :[数组]输入数组. axis : ...
- python中argmin函数_python中argmin函数_Python numpy.argmin()用法及代码示例
numpy.argmin(array,axis = None,out = None):返回特定轴上数组min元素的索引. 参数: array:Input array to work on axis : ...
- python memory usage_Python Pandas Index.memory_usage()用法及代码示例
Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas Index.memory_ ...
- python barh_Python matplotlib.axes.Axes.barh()用法及代码示例
Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展.轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系. Axes实例通过 ...
- python axes_Python Matplotlib.axes.Axes.axis()用法及代码示例
Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展.轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系. Axes实例通过 ...
- or函数 java_Java OptionalInt orElseGet()用法及代码示例
orElseGet(java.util.function.IntSupplier)方法可帮助我们获取此OptionalInt对象中的值.如果此OptionalInt中不存在值,则此方法返回提供函数产生 ...
最新文章
- HDLBits 系列(12)All about Karnaugh Map
- hex文件和bin文件区别
- 抛硬币直到若干次(k次)连续正面向上的概率
- 安卓摄像头 data 转bitmap
- svn 不支持http 客户端_Xversion for mac(SVN客户端)
- 通过递归算法完成树的级联勾选的一般思路
- DCMTK:读取DICOM数据集的测试程序
- ftp协议分析(一)
- ArcGIS 10.6连接Access 2007(.accdb)及以上版本数据库
- MySQL数据库的自动备份与数据库被破坏后的恢复
- 对于python初学者,如何使用python定义联合(使用ctypes)?
- 设置行间距_word打印技巧:几个节省纸张的打印设置方法
- java简单巡回置换算法程序代码_巡回置换算法(巡回置换算法实现流程)
- HCIA RS题库及解析(2018版)
- 计算机软考软件设计师2019试题,软考试题及答案【2018计算机软考软件设计师考前练习试题及答案】...
- Discuz X 3.4 系列漏洞梳理
- 敏捷技术和管理方法思考列表---长期维护
- c语言 continue什么意思,continue在C语言中什么意思?
- 基于python中jieba包的中文分词中详细使用(一)
- 2021 China Open Source Annual Report