Python
uWSGI & WSGI & uwsgi
Development Environment
01.Python运行的虚拟环境
02.pyenv
01.pyenv-pip-migrate
02.pyenv-pip-rehash
03.IPython and Jupyter Notebook
04.Pycharm
05.pip
06.Python 缓存镜像源服务器
07.Python环境的迁移
Python Docs
01.进制与码制
02.基础数据结构
03.Python程序流程控制结构
04.字符串
05.数据结构
06.Python 内存管理
07.封装和解构
08.列表解析、生成器、迭代器
09.Python 中的函数
10.函数的作用域
11.高阶函数与装饰器
python 包管理
Annotations
refection
Python PEP—Python增强提案
Python 中的错误和异常
namedtuple
魔术方法 可视化
mixin
Python 面向对象
super
Monkey Patch
Python 析构函数
Python中 _ 和 __
property
Class
三元表达式,列表解析 和 生成器表达式
random
使用Python+Fabric实现Linux自动化操作
Note
modle
Paramiko 模块
CX_ORACLE 模块
collections 模块
python-docx
UnboundLocalError
Django
00.Django 安装
08.Django Admin 管理工具
09.数据库配置
07.Django 路由
05.Django 视图
Django REST Framework
Django 管理工具
04.Django 表单
02.Django 模板
01.创建项目
06.Django 视图
03.Django 模型
0301.单表实例
Python+Flask+uWSGI 的Docker服务脚本
nameko
PyFlink DataStream API - state & timer
从oracle同步数据到mysql
Exercises
Task 01.list
Task 02.排序
本文档使用 MrDoc 发布
-
+
home page
random
# 1.random.randint 和 random.randrange 的区别 1. randint 是左右闭区间 ,而randrange取值是左闭右开,即取不到最右边的值 1. randint 只是随机产生某个区间内的一个值,但是randrange则可以按固定的间隔来产生随机数 区别1:randint 是左右闭区间 ,而randrange取值是左闭右开,即取不到最右边的值 ``` >>> import random >>> lst = [random.randint(1,3) for list in range(10)] >>> print('randint结果:',lst) randint结果: [2, 2, 3, 2, 1, 3, 2, 2, 3, 3] >>> ``` randint 相当于 [] 区间 ``` >>> import random >>> lst = [random.randrange(1,3) for lst in range(10)] >>> print('randrange结果:', lst) randrange结果: [2, 2, 1, 2, 2, 2, 1, 2, 2, 1] >>> ``` randrange 相当于 [ )区间 > 在randint 的取值中是有 3 的,但是randrange 的取值中是没有 3 的 区别2:randint 只是随机产生某个区间内的一个值,但是randrange则可以按固定的间隔来产生随机数 ``` >>> import random >>> >>> lst1 = [random.randrange(2,101,2) for lst1 in range(10)] >>> lst2 = [random.randrange(1,101,2) for lst2 in range(10)] >>> print('偶数序列', lst1) 偶数序列 [60, 44, 50, 38, 40, 100, 58, 100, 36, 100] >>> print('奇数序列',lst2) 奇数序列 [79, 25, 37, 3, 79, 15, 67, 89, 79, 13] >>> ``` # 2.random.shuffle random.shuffle()用于将一个列表中的元素打乱顺序,值得注意的是使用这个方法不会生成新的列表,只是将原列表的次序打乱。 ``` # shuffle()使用样例 import random x = [i for i in range(10)] print(x) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] random.shuffle(x) print(x) [2, 5, 4, 8, 0, 3, 7, 9, 1, 6] ``` 源码及注释 ``` def shuffle(self, x, random=None): """Shuffle list x in place, and return None. 原位打乱列表,不生成新的列表。 Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used. 可选参数random是一个从0到参数的函数,返回[0.0,1.0)中的随机浮点; 如果random是缺省值None,则将使用标准的random.random()。 """ if random is None: randbelow = self._randbelow for i in reversed(range(1, len(x))): # pick an element in x[:i+1] with which to exchange x[i] j = randbelow(i + 1) x[i], x[j] = x[j], x[i] else: _int = int for i in reversed(range(1, len(x))): # pick an element in x[:i+1] with which to exchange x[i] j = _int(random() * (i + 1)) x[i], x[j] = x[j], x[i] ``` # 3.random.choice() - random.choice 1. 从一个数组中选择一个数据并返回 ``` str = 'adsf' #返回str中任意一个元素 result = random.choice(str) ``` 1. 在数组ARRAY中随机取n个index ``` idx = [np.random.choice(len(ARRAY)) for _ in range(10)] ``` - random.choices 1. 从一个数组中选择任意n个数据(可重复)并返回,默认情况下为一个 ``` import random str = 'asdrf' print(random.choices(str,k=3)) ``` 使用 weights 來控制样本出现的频率 ``` import random str = 'asdrf' print(random.choices(str,[100,1,10],k=3)) ``` weights 不能大于样本值,ValueError: The number of weights does not match the population。 # 4.random.sample 采样 ``` >>> import random >>> for i in range(10): ... print(random.sample([0,1],k=2)) ... [1, 0] [0, 1] [0, 1] [1, 0] [0, 1] [1, 0] [1, 0] [1, 0] [0, 1] [0, 1] >>> ``` 从样本中**不重复的取值**,key值不能大于样本数。 ``` >>> import random >>> for i in range(10): ... print(random.sample([0,1,1,1,0,0],k=2)) ... [0, 0] [1, 0] [1, 0] [1, 1] [1, 1] [1, 0] [1, 0] [1, 0] [0, 0] [1, 0] >>> ``` > 不重复指的是从样本中取出不通index的值不重复。
Seven
March 12, 2023, 1:47 p.m.
转发文档
Collection documents
Last
Next
手机扫码
Copy link
手机扫一扫转发分享
Copy link
Markdown文件
share
link
type
password
Update password