【问题标题】:'TypeError: 'builtin_function_or_method' object is not subscriptable' in binary searches'TypeError:'builtin_function_or_method'对象不可下标'在二进制搜索中
【发布时间】:2018-08-04 23:41:41
【问题描述】:

对于作业,我必须进行二进制搜索,我按照步骤操作,但出现以下错误:

Traceback(最近一次调用最后一次):文件“X:/Computer Science/Python/February 2018/25 02 18/binary search.py​​”,第 15 行, 在 end = aList.index[middle] TypeError: 'builtin_function_or_method' 对象不可下标

这是我的代码:

aList = [3,13,24,27,31,39,45,60,69]
end = len(aList) - 1
found = 0
start = 0

target = int(input("Please input the item you would like to search for:\t>"))
while start <= end and found == 0:
    middle = int((start + end) / 2)

    if aList[middle] == target:
        found = 1
        print(target , "is in the list.")

    if target < aList[middle]:
        end = aList.index[middle]
    else:
        start = aList.index[middle]
if found == 0:
    print("The search item was not found.")

【问题讨论】:

    标签: typeerror binary-search


    【解决方案1】:

    请看这里In Python, what does it mean if an object is subscriptable or not?

    我解释一下:“[成为可下标的对象必须]实现 getitem() 方法”

    index[middle] 的意思是“从字典index 中提取键值等于变量middle 的值”

    第 15 行应该如下所示 - 一个函数调用:

    end = aList.index(中间)

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 2020-02-03
      • 2020-11-05
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      相关资源
      最近更新 更多