1 回答
TA贡献1820条经验 获得超2个赞
您会看到重复的 C,因为当前节点的索引未正确计算。
这是带有注释的更新代码:
class Chain:
def __init__(self, probabilities, start): # only called once
self.probs = probabilities
self.start = start
self.names = list(self.probs.keys())
def __iter__(self): # only called once
return self
def __next__(self): # each iteration
import random
self.pos = self.start # next step
self.random_num = random.randrange(100) # choice percentile must be in here
i = self.names.index(self.pos) # get index of this node in big list
prob_l = self.probs[self.pos] # get probs
self.prob_sum = 0 # start prob scan
for ind, prob in enumerate(prob_l): # probs of going to another node
self.prob_sum += prob # until 100%
if self.random_num < self.prob_sum: # passed percentile, go to another node
exclude_names = self.names[:i] + self.names[i + 1:] # big list without this node
self.start = exclude_names[ind] # for next iteration
break # found percentile in probs
return self.pos # add current pos to chain
chain = Chain({"A": [50, 25], "B": [50, 25], "C": [50, 50]}, "A")
chain_iter = iter(chain)
for k in range(100):
print(next(chain_iter), end=" ")
输出(包装)
A B A A B B C B A B A C A B C B A C B A B C B B C
B A B C B A C A B A A B C B B A B B A B A C B C B
B C A B A C A C B A C A B A B A C A B B C B A B A
C A C A C B A A C B A A B B B A A A C A A B A B B
添加回答
举报