【问题标题】:Is a dictionary membership check in python a linear operation? [duplicate]python中的字典成员资格检查是线性操作吗? [复制]
【发布时间】:2016-10-24 17:08:11
【问题描述】:

python 中的字典成员检查是 O(n) 中的线性运算吗? 例如:

Dct = {"a":0,"b":1,"c":2,"d":3}
if d in Dct:
    #do something

【问题讨论】:

标签: python dictionary


【解决方案1】:

没有。

字典被实现为哈希表,因此哈希计算需要时间,查找时间仅为 O(1)。

dict 还会根据内存大小自动优化自身,以防止出现大桶。

有关哈希表的更多信息,您可以查看Wikipedia article

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多