在python 2.7中调用字典items()函数时的KeyError

时间:2018-01-16 07:28:29

标签: python dictionary

以下是例外信息:

  File "/usr/lib/python2.7/collections.py", line 113, in items
    return [(key, self[key]) for key in self]
exceptions.KeyError: u'abc'

我认为如果密钥abc不在dict中,则不应显示密钥for key in self,因此abc不会返回ordered_dict = collections.OrderedDict() ... do something to add elements to ordered_dict def foo(): for k, b in ordered_dict.items(): if time.time() - b > 10: del ordered_dict[k] else: twisted.internet.reactor.callLater(1, foo) def bar(k): ordered_dict[k] = time.time() ... bar() is called constantly to add values, and foo() is called at the beginning of the program

代码是这样的:

KeyError

这里有什么不对吗? twisted随机发生,并非总是如此。

python版本2.7.3 使用timeX = t r v a 4 7 3 b 0 1 8 c 0 7 9 d 9 6 0 e 1 3 4 f 8 7 2 g 5 7 4 h 5 1 0 i 9 8 6 j 4 6 7 模块

0 个答案:

没有答案