python中的迭代

时间:2018-07-02 15:23:50

标签: python python-3.x

例如,对于 [p1,p2,p3] = [0.4,0.3,0.8] ,并且k = 2。的2个子集 {1,2,3} 以及相应的概率 习 是我的一个 在子集中是

{1,2} = 0.024 = (0.4x0.3x(1-0.8))

{1,3} = 0.224 = (0.4x0.8x(1-0.3))

{2,3} = 0.144 = (0.3x0.8x(1-0.4))

{1,2} = 0.024

{1,3} = 0.224

{2,3} = 0.144 因此,

P(X = k)= 0.024 + 0.224 + 0.144 = 0.392。

什么是 python代码?我尝试使用itertools.combination但它 在0x00000218F0129A48显示 itertools.combinations对象

1 个答案:

答案 0 :(得分:1)

它可能很笨重,但是可以满足您的需求。

H

输出:

import itertools
a = [0.4,0.3,0.8]
l = list(itertools.combinations(a,len(a)-1))
print(l)
x = []
while l:
    b = l.pop()
    for item in a:
        if not item in b:
            multiply = 1
            for s in b:
                multiply = multiply * s
            x.append(multiply * (1 - item))
            print(x[-1])

print(sum(x))