python概率计算

来源:互联网 发布:stc52单片机引脚功能 编辑:程序博客网 时间:2024/06/10 06:56



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 == 0D = {1, 2, 3, 4, 5, 6}print( such_that(even, D))

原创粉丝点击