【发布时间】:2017-09-24 17:42:52
【问题描述】:
我的理解是 .items() 仅适用于 python 字典。
但是,在以下完美运行的代码中,似乎 .items() 函数可用于字符串。 (此代码用于 doc2vec 的预处理阶段)
我已经研究了一段时间,但我不明白为什么 .items() 似乎在这段代码中起作用。
在代码中,'sources' 只是实例的一个属性。然而它能够调用 .items()。
我在这里缺少什么?
class LabeledLineSentence(object):
def __init__(self, sources):
self.sources = sources
flipped = {}
# make sure that keys are unique
for key, value in sources.items():
if value not in flipped:
flipped[value] = [key]
else:
raise Exception('Non-unique prefix encountered')
【问题讨论】:
-
代码中没有任何内容暗示
sources(因此self.sources)不是dict-like对象。为什么你认为它是一个字符串?
标签: python loops dictionary