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 以指定的概率选取元素的更多相关文章

  1. python删除指定位置 2个元素

    # -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#排序说明:http://en.wikipedia.org/wiki/i ...

  2. python读取指定字节长度的文本

    软件版本 Python 2.7.13;   Win 10 场景描述 1.使用python读取指定长度的文本: 2.使用python读取某一范围内的文本. Python代码 test.txt文本内包含的 ...

  3. python获取指定目录下所有文件名os.walk和os.listdir

    python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...

  4. 11月8日上午Jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件

    jquery基础知识 1.jquery文件的引入,所有的js代码要写在下面那段代码下面. <script src="../jquery-1.11.2.min.js">& ...

  5. js指定分隔符连接数组元素join()

    指定分隔符连接数组元素join() join()方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. 语法: arrayObject.join(分隔符) 参数说明: 注意:返回 ...

  6. jquery简单原则器(匹配除了指定选择器之外的元素 selector 表示选择器)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Web测试Selenium:如何选取元素

    Web测试工具Selenium:如何选取元素 2009-02-17 23:23 by 敏捷的水, 5372 阅读, 22 评论, 收藏, 编辑 selenium是一个非常棒的Web测试工具,他对Aja ...

  8. Python压缩指定文件及文件夹为zip

    Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...

  9. python 遍历list并删除部分元素

    python 遍历list并删除部分元素https://blog.csdn.net/afgasdg/article/details/82844403有两个list,list_1 为0-9,list_2 ...

随机推荐

  1. hdu 5204 Rikka with sequence 智商不够系列

    Rikka with sequence Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  2. linux基础命令学习 (十一)系统管理命令

    stat              显示指定文件的详细信息,比ls更详细 who               显示在线登陆用户 whoami          显示当前操作用户 hostname   ...

  3. Two-transistor circuit replaces IC

    Linear Technology's recently introduced LTC4300 chip buffers I2C clock and data lines to and from a ...

  4. ADS-B显示终端6.0

    改动日志 1  更新背景地图. 增加了全国范围内的飞行限制区.飞行危急区.限制区採用黄色区域表示.危急区採用红色区域表示.全部原始资料均来自民航局发布的航行情报资料汇编. 2为解决显示元素过多,屏幕显 ...

  5. [c#基础]泛型集合的自定义类型排序

    引用 最近总有种感觉,自己复习的进度总被项目中的问题给耽搁了,项目中遇到的问题,不总结又不行,只能将复习基础方面的东西放后再放后.一直没研究过太深奥的东西,过去一年一直在基础上打转,写代码,反编译,不 ...

  6. End of Life check fails with NullPointerException

    Checks if the running version of JIRA is approaching, or has reached End of Life. Details Type: Bug ...

  7. ASP.NET Core 1.0基础之应用启动

    来源https://docs.asp.net/en/latest/fundamentals/startup.html ASP.NET 5 使得应用对每个http请求有完整的控制权.Startup类是程 ...

  8. poi 技术动态更新 Excel模板内容,动态更新内容

    1.控制器方法 private URL base = this.getClass().getResource(""); /** * 流拍之后,可以下载询价单 * * @param ...

  9. linux有用技巧:使用ntfs-3g挂载ntfs设备

    1.几种文件系统的比較 (1)在linux系统中支持一下文件系统:               Ext2         第二扩展文件系统(简称 ext2 或者 ext2) 非常多年前就已经成为 GN ...

  10. iOS: 详细的正则表达式

    一.简单的正则规则 1.由数字.26个英文字母或者下划线组成的字符串: ^[-9a-zA-Z_]{,}$ 2.非负整数(正整数 + 0 ): ^/d+$ 3. 正整数: ^[-]*[-][-]*$ 4 ...