Python 以指定的概率选取元素
Python 以指定的概率选取元素
Problem
You want to pick an item at random from a list, just about as random.choice
does, but you need to pick the various items with different probabilities given in another list, rather than picking any item with equal probability as random.choice
does.
Solution
Module random
in the standard Python library offers a wealth of possibilities for generating and using pseudo-random numbers, but it does not offer this specific functionality, so we must code it as a function of our own:
- import random
- def random_pick(some_list, probabilities):
- x = random.uniform(0,1)
- cumulative_probability = 0.0
- for item, item_probability in zip(some_list, probabilities):
- cumulative_probability += item_probability
- if x < cumulative_probability:
- break
- return item
random.uniform(0,1)->生成0.0到1.0之间的伪随机数,之后循环元素及其概率,计算累积概率.
如:random_pick([1,2,3,4],[0.1,0.2,0.3,0.4])
当x处于0.0到0.1之间,则输出1
当x处于0.1到0.3之间,则输出2
……
Example
- #以指定的概率获取元素 以一个列表为基准概率,从一个列表中随机获取元素
- import random
- def random_pick(some_list, probabilities):
- x = random.uniform(0,1)
- cumulative_probability = 0.0
- for item, item_probability in zip(some_list, probabilities):
- cumulative_probability += item_probability
- if x < cumulative_probability:break
- return item
- some_list = [1,2,3,4]
- probabilities = [0.2,0.1,0.6,0.1]
- print random_pick(some_list,probabilities)
【注意】要求 some_list 的长度和 probabilities 的长度一致,以及所有元素的概率相加为1.0
【References】
[1] Randomly Picking Items with Given Probabilities
[2] Python 以指定概率获取元素
Python 以指定的概率选取元素的更多相关文章
- python删除指定位置 2个元素
# -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#排序说明:http://en.wikipedia.org/wiki/i ...
- python读取指定字节长度的文本
软件版本 Python 2.7.13; Win 10 场景描述 1.使用python读取指定长度的文本: 2.使用python读取某一范围内的文本. Python代码 test.txt文本内包含的 ...
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- 11月8日上午Jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件
jquery基础知识 1.jquery文件的引入,所有的js代码要写在下面那段代码下面. <script src="../jquery-1.11.2.min.js">& ...
- js指定分隔符连接数组元素join()
指定分隔符连接数组元素join() join()方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. 语法: arrayObject.join(分隔符) 参数说明: 注意:返回 ...
- jquery简单原则器(匹配除了指定选择器之外的元素 selector 表示选择器)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Web测试Selenium:如何选取元素
Web测试工具Selenium:如何选取元素 2009-02-17 23:23 by 敏捷的水, 5372 阅读, 22 评论, 收藏, 编辑 selenium是一个非常棒的Web测试工具,他对Aja ...
- Python压缩指定文件及文件夹为zip
Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...
- python 遍历list并删除部分元素
python 遍历list并删除部分元素https://blog.csdn.net/afgasdg/article/details/82844403有两个list,list_1 为0-9,list_2 ...
随机推荐
- Digital controller compensates analog controller
Emerging digital ICs for power control lack basic features, such as the built-in gate drive and curr ...
- How to create functions that can accept variable number of parameters such as Format
http://www.chami.com/tips/delphi/112696D.html Sometimes it's necessary to pass undefined number of [ ...
- QMsgPack的用法DEMO
QMsgPack的用法DEMO 引用单元文件: uses qstring, qmsgpack, qjson; 演示一: procedure TForm2.Button10Click(Sender: T ...
- C# 如何做类似微博和QQ的授权登录
记录下吧,dotnet下有这个http://dotnetopenauth.net/开源的框架可以用来实现服务端和客户端的实现,具体的使用方法还真研究,第一次接触OAuth这个概念
- android DrawerLayout 侧边栏实现
现在实现侧边栏比较简单了,官方提供的DrawerLayout可以很方便实现. 主要实现方法是:用DrawerLayout 作为界面根控件.在DrawerLayout里面第一个View为当前界面主内容: ...
- WebLogic中WLS 组件漏洞(CVE-2017-10271)专项检测工具
来源: 时间:2017-12-23 00:00:00 作者: 浏览:1929 次 近期安恒信息在应急响应过程中发现有恶意攻击者利用WebLogic漏洞对企业服务器发起大范围远程攻击,攻击成功后植入挖矿 ...
- react组件引用时的default常见错误
1.下面使用方法是正确的: export class StepLoad extends React.Component {} 引用上面插件的方法,重命名 import {StepLoad as Ste ...
- 关于TagHelper的那些事情——自定义TagHelper(内嵌TagHelper)
内嵌TagHelper 上一篇文章中提到有时候需要设计一种内嵌的TagHelper,如下: <my name="yy" age="35"> < ...
- Centos 7 配置 VNCServer 經驗
安裝 Centos 7後, 習慣性的安裝 Xmanager 3或4, 都不能正常工作, 無奈之下開始安裝 VNCServer. (個人習慣使用Xmanager, 因為不需要安裝,只要配置一下就能用, ...
- 使用PowerDesigner建立数据库模型【转】
1. 打开PowerDesigner,点击File->New 2. 选择Conceptual Data Model,并修改Model name. 3. 在Palette工具栏中点击Entity ...