python - “builtin_function_or_method”对象不可订阅

标签 python algorithm

def binary_search(li, targetValue):
    low, high = 0, len[li] #error on this line
    while low <= high:
        mid = (high - low)/2
        if li[mid] == targetValue:
             return "we found it!"
        elif li[mid] > targetValue:
             low = mid - 1;
        elif li[mid] < targetValue:
             high = mid + 1;
    print "search failure "

最近刚刚发布了这个问题,但我的代码仍然不起作用?

最佳答案

你用错了括号 len(li) 而不是 len[li]

请记住,当您尝试访问一个函数时,您需要使用 function(args),如果您使用 [],您实际上是在访问一个序列,如列表。 你的列表[索引]。 len 是内置函数,因此您需要 ()

关于python - “builtin_function_or_method”对象不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19989606/

相关文章:

Python 异常处理 - 避免编写 30+ try except block

python - 循环创建多个图形后如何保存它们?

algorithm - 比较给定步骤的两种算法的复杂性

algorithm - 向量化搜索包含给定子排列(带重复)的排列(带重复)

python - 如何在 python 和 C++ 中以毫秒为单位打印相同的时间?

python - 用while循环python写阶乘

algorithm - Elo 对接会也考虑等待时间

php - 彩票算法 - PHP - 数学看起来不错,但函数有效吗?

python - python中的时区处理

javascript - 在 O(n) 时间内对 (1,2,3) 个数字的数组进行排序