我在使用字典时遇到了一些奇怪的问题,我正在尝试从字典中迭代对以传递给另一个函数。尽管出于某种原因,迭代器的循环总是返回空值。
这是代码:
def LinktoCentral(self, linkmethod):
if linkmethod == 'sim':
linkworker = Linker.SimilarityLinker()
matchlist = []
for k,v in self.ToBeMatchedTable.iteritems():
matchlist.append(k, linkworker.GetBestMatch(v, self.CentralDataTable.items()))
现在,如果我在 for 循环上方插入打印行:
matchlist = []
print self.ToBeMatchedTable.items()
for k,v in self.ToBeMatchedTable.iteritems():
matchlist.append(k, linkworker.GetBestMatch(v, self.CentralDataTable.items()))
我得到了应该在字典中打印的数据。字典的值是列表对象。我在 for 循环上方打印时从字典中获得的示例元组:
>>> (1, ['AARP/United Health Care', '8002277789', 'PO Box 740819', 'Atlanta', 'GA','30374-0819', 'Paper', '3676'])
但是,for 循环为 linkworker.GetBestMatch 方法提供了空列表。如果我在 for 循环正下方放置一条打印线,我会得到:
代码:
matchlist = []
for k,v in self.ToBeMatchedTable.iteritems():
print self.ToBeMatchedTable.items()
matchlist.append(k, linkworker.GetBestMatch(v, self.CentralDataTable.items()))
## Place holder for line to send match list to display window
return matchlist
第一次迭代的结果:
>>> (0, ['', '', '', '', '', '', '', ''])
我真的不知道发生了什么,执行此循环时没有其他任何事情发生。我犯了什么愚蠢的错误?