首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python概率计算

python概率计算

作者头像
py3study
发布2020-01-10 11:34:06
1.2K0
发布2020-01-10 11:34:06
举报
文章被收录于专栏:python3python3
from fractions import Fraction
# # from __future__ import division
# def P(event, space):
#     "在一个等可能发生的样本空间中,事件发生的概率"
#     return Fraction(len(event & space), len(space))
#
# D = {1, 2, 3, 4, 5, 6}
# even = {2, 4}
# aaa= P(even, D)
# print(aaa)


def P(event, space):
    """在一个等可能发生的样本空间中,事件发生的概率   . 事件可以是输出值的集合,或者是一个断言(属于事件的输出值为真)"""
    if callable(event):
        event = such_that(event, space)
    return Fraction(len(event & space), len(space))

def such_that(predicate, collection):
    "集合中满足断言为真的元素构成的子集"
    return {e for e in collection if predicate(e)}

def even(n): return n%2 == 0

D = {1, 2, 3, 4, 5, 6}
print( such_that(even, D))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档