我不明白这一行q.append(p[i] * (hit * pHit + (1-hit) * pMiss))
,因为变量hit
是一个布尔值。该布尔值来自hit = (Z == world[i])
那里发生了什么事?我对Python只有一个基本的了解......
p = [0.2, 0.2, 0.2, 0.2, 0.2]
world = ['green', 'red', 'red', 'green', 'green']
Z = 'red'
pHit = 0.6
pMiss = 0.2
def sense(p, Z):
q=[]
for i in range(len(p)):
hit = (Z == world[i])
q.append(p[i] * (hit * pHit + (1-hit) * pMiss))
s = sum(q)
for i in range(len(p)):
q[i]=q[i]/s
return q
print sense(p,Z)