简体   繁体   English

python排序错误:TypeError:'list'对象不可调用

[英]python sorting error: TypeError: 'list' object is not callable

I am trying to sort a list of list based on the first item of each element:我正在尝试根据每个元素的第一项对列表列表进行排序:

def getKey(item):
    return item[0]    

def myFun(x):
    return sorted(x, key= getKey(x))

my_x = [[1,100], [5,200], [3,30]]

myFun(my_x)

I want the sorting based on the first item of each element, ie 1, 5 and 3. The expected result should be: [[1,100], [3,30], [5,200]]我想根据每个元素的第一项进行排序,即 1、5 和 3。预期结果应该是: [[1,100], [3,30], [5,200]]

However, I got the error below:但是,我收到以下错误:

TypeErrorTraceback (most recent call last)
<ipython-input-7-4c88bbc1a944> in <module>()
      3 
      4 my_x = [[1,100], [5,200], [3,30]]
----> 5 myFun(my_x)

<ipython-input-7-4c88bbc1a944> in myFun(x)
      1 def myFun(x):
----> 2     return sorted(x, key= getKey(x))
      3 
      4 my_x = [[1,100], [5,200], [3,30]]
      5 myFun(my_x)

TypeError: 'list' object is not callable

Any idea what I did wrong here?知道我在这里做错了什么吗? Thanks!谢谢!

The problem is that you're assigning the result of getKey(x) to key=getKey(x) .问题是您将getKey(x)的结果分配给key=getKey(x) You want to assign the function pointer.您要分配函数指针。

def getKey(item):
    return item[0]    

def myFun(x):
    return sorted(x, key=getKey)

my_x = [[1,100], [5,200], [3,30]]

myFun(my_x)

Having said that, if you're just sorting based on the first element, that's the default behavior of sorted .话虽如此,如果您只是根据第一个元素进行排序,那么这就是sorted的默认行为。

You can just do sorted(x) .你可以只做sorted(x)

You don't need to call the function getKey .您不需要调用函数getKey The signature of sorted requires you pass the key argument as a callable: sorted的签名要求您将key参数作为可调用对象传递:

sorted(x, key=getKey)

getKey is a function, (a type of "callable" object). getKey是一个函数,(一种“可调用”对象)。 Its output, getKey(x) , on the other hand, is a list object which is not callable.另一方面,它的输出getKey(x)是一个不可调用的list对象。 Your mistake is that you're setting key=getKey(x) and hence assigning a list object to the argument key , whereas sorted expects something callable attached to that name.您的错误是您正在设置key=getKey(x)并因此将list对象分配给参数key ,而sorted期望附加到该名称的可调用内容。 So that explains why, when the internal sorting code tries to call your key , it fails with the error "'list' object is not callable".所以这就解释了为什么当内部排序代码尝试调用您的key ,它会失败并显示错误“'list' object is not callable”。 Really, you should have just said key=getKey .真的,你应该说key=getKey

UPDATE更新

sorted 's key attribute should be callable, so instead of assigning key=getKey() you need to key=getKey sortedkey属性应该是可调用的,所以不是分配key=getKey()你需要key=getKey

INITIAL ANSWER (could be useful if you want to make code look better):初始答案(如果您想让代码看起来更好,可能会很有用):

There is no such method as getKey , but没有像getKey这样的方法,但是there is itemgetter from operator package有来自operator包的itemgetter

from operator import itemgetter


def myFun(x):
    return sorted(x, key=itemgetter(0))

my_x = [[1,100], [5,200], [3,30]]

myFun(my_x)

To the downvoters :致反对者

It's clearly said that已经明确说了

I want the sorting based on the first item of each element我想根据每个元素的第一项进行排序

And if you think that you would sort presented list only with sorted() , you are wrong.如果您认为只能使用sorted()对呈现的列表进行sorted() ,那您就错了。

>>> my_x = [[1,100], [5,200], [3,30], [1,30]]
>>> myFun(my_x)
[[1, 100], [1, 30], [3, 30], [5, 200]]
>>> sorted(my_x)
[[1, 30], [1, 100], [3, 30], [5, 200]]

So sorted is not based only on first argument, as OP wanted如此sorted不仅基于第一个参数,正如 OP 所希望的

INITIAL ANSWER'S UPDATE初始答案的更新

Also, when i answered there was no getKey method, you can look at OP post edit history另外,当我回答没有getKey方法时,您可以查看OP 后编辑历史记录

And so OP post now have getKey method you can look at the appropriate @MosesKoledoye 's answer所以 OP 帖子现在有getKey方法,您可以查看适当的@MosesKoledoye 的答案

If you are coming here because you got the exact same error as mentioned in the question如果您来这里是因为您遇到了与问题中提到的完全相同的错误

TypeError: 'list' object is not callable

chances are you simply did something like the following:您可能只是做了以下操作:

sorted = sorted(someList) 

The error will go away if the variable name is changed from sorted to something else, like如果变量名称从 sorted 更改为其他名称,则错误将消失,例如

sortedList = sorted(someList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM