gpt4 book ai didi

python - list.index(value)执行什么样的查找?

转载 作者:行者123 更新时间:2023-11-30 23:37:35 24 4
gpt4 key购买 nike

当我调用时,python 在幕后做什么:

very_big_list.index(value)

在字符串列表上?

它比等效的字典查找更有效吗?

d = {'hello':1, 'dog':2, 'cat':3, ...}
print d['dog']

最佳答案

.index() 循环遍历列表,直到找到相等的元素(element == searchterm 为 True)。字典查找效率更高

在 CS 术语中,.index() 的复杂度为 O(n),字典查找的复杂度为 O(1)。请参阅Time Complexity overview在 Python Wiki 上。

关于python - list.index(value)执行什么样的查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15397630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com