【发布时间】:2019-01-18 21:41:07
【问题描述】:
在 python 中从 python dict 或 defaultdict 中删除一个键是 O(1) 操作,as mentioned here 和 here。要从OrderedDict 中删除密钥,我们可以使用del d[key] 或使用popitem() 方法,如the docs 中所述。
OrderedDict的底层实现和del操作的时间复杂度是多少?
编辑:这个答案 OrderedDict performance (compared to deque) 指的是 OrderedDict 中 del 的复杂度为 O(1)。但是,我们如何在实现细节层面证明它的合理性?
【问题讨论】:
-
OrderedDict performance (compared to deque) 的可能重复项。虽然问题不同,但那里的答案也回答了您的问题。
-
已编辑以包含它的不同之处,以及我正在寻找的细节,但我找不到。
-
OrderedDict类是在 Python 中实现的(在普通的dict之上),因此您可以通过go directly to the source 了解它是如何实现的。顺序由双向链表维护。任何键的链接都可以在普通字典中有效地查找。
标签: python python-3.x dictionary ordereddictionary