【发布时间】:2017-07-16 02:48:15
【问题描述】:
这是错误信息:
RuntimeError: 迭代期间字典大小改变
这是我的代码段(
# Probability Distribution from a sequence of tuple tokens
def probdist_from_tokens (tokens, N, V = 0, addone = False):
cfd = ConditionalFreqDist (tokens)
pdist = {}
for a in cfd: # <= line with the error
pdist[a] = {}
S = 1 + sum (1 for b in cfd[a] if cfd[a][b] == 1)
A = sum (cfd[a][b] for b in cfd[a])
# Add the log probs.
for b in cfd[a]:
B = sum (cfd[b][c] for c in cfd[b])
boff = ((B + 1) / (N + V)) if addone else (B / N)
pdist[a][b] = math.log ((cfd[a][b] + (S * boff)) / (A + S))
# Add OOV for tag if relevant
if addone:
boff = 1 / (N + V)
pdist[a]["<OOV>"] = math.log ((S * boff) / (A + S))
return pdist
我基本上只是使用 cfd 作为参考,将正确的值放入 pdist。我不是想改变 cfd,我只是想迭代它的键和它的子字典的键。
我认为问题是由我设置变量 A 和 B 的行引起的,当我在这些行上有不同的代码时,我得到了同样的错误,但是当我用常量值替换它们时我没有得到错误.
【问题讨论】:
-
你能提供一个独立的例子来说明这个问题吗?
标签: python python-3.x dictionary nltk