在dictionary.values()列表与集合中查找的时间复杂度

Sri*_*ram 6 python performance big-o dictionary hashmap

在Python中,我们知道在字典中查找键需要O(1)的运行时间,但在dictionary.values()中查找的运行时间是多少?

dictionary = {'a':[66,77,88], 'b':[99,100]}
key = 'a'
if key in dictionary: # takes O(1) run time 

number = '99'
if number in dictionary.values():  # What is the run time here?
Run Code Online (Sandbox Code Playgroud)

编辑#1:键的值可以是列表或集合。许多人回答说,如果列出值,则运行时间为O(1)。

如果设置了值,它将是O(N)吗?

dictionary = {'a':(66,77,88), 'b':(99,100)}
number = '99'
if number in dictionary.values():  # What is the run time here?
Run Code Online (Sandbox Code Playgroud)

BPL*_*BPL 6

令 为x in s在列表中搜索的操作,{x=item , s=list}

平均情况 - 假设参数随机均匀生成 - 对于此类操作将是 O(n)

有关时间复杂度的更多信息,这里是官方链接