Python 'builtin_function_or_method' 对象不可下标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19989606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:10:22  来源:igfitidea点击:

'builtin_function_or_method' object is not subscriptable

pythonalgorithm

提问by user2928929

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 "

just posted this question recently, but my code still doesn't work?

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

回答by jramirez

you are using the wrong brackets len(li)not len[li]

您使用了错误的括号,len(li)而不是len[li]

Remember when you are trying to access a function you need to use function(args)if you use []you are actually accessing a sequence like a list. your_list[index]. len is a built in function so you need the ()

请记住,当您尝试访问一个需要使用的函数时,function(args)如果您[]实际上是在访问一个像列表这样的序列。your_list[index]. len 是一个内置函数,所以你需要()

回答by jramirez

Python uses (...)to call a function and [...]to index a collection. Furthermore, what you are trying to do now is index the built-in function len.

Python 用于(...)调用函数和[...]索引集合。此外,您现在要做的是索引内置函数len

To fix the problem, use parenthesis instead of square brackets:

要解决此问题,请使用括号而不是方括号:

low, high = 0, len(li)

回答by Martijn Pieters

lenis a built-in function, but you are trying to use it as a sequence:

len是一个内置函数,但您试图将其用作序列:

len[li]

Callthe function instead:

改为调用该函数:

len(li)

Note the shape change there, indexing is done with square brackets, calling is done with round parentheses.

注意那里的形状变化,索引是用方括号完成的,调用是用圆括号完成的。

回答by Doogle

It took me couple of minute to figure out what is the mistake. Sometimes a blindspot stops you from looking at obvious.

我花了几分钟才弄清楚错误是什么。有时盲点会阻止你看明显的东西。

Incorrect

不正确

msg = "".join['Temperature out of range. Range is between', str(
            HeatedRefrigeratedShippingContainer.MIN_CELSIUS), " and ", str(
            RefrigeratorShippingContainer.MAX_CELSIUS)]

Correct

正确的

msg = "".join(['Temperature out of range. Range is between', str(
        HeatedRefrigeratedShippingContainer.MIN_CELSIUS), " and ", str(
        RefrigeratorShippingContainer.MAX_CELSIUS)])

As you can see join is a method and has to be called with () which was missing and causing the issue. Hope it helps all to look for the method and add ().

如您所见, join 是一种方法,必须使用 () 调用,该方法丢失并导致问题。希望对大家找方法和加()有所帮助。