【问题标题】:how to solve list is not callable error in python?如何解决python中的list is not callable错误?
【发布时间】:2016-03-13 06:13:56
【问题描述】:

我是 python 新手,我正在尝试根据值对字典进行排序 我写了什么:

         sorted_dictionary = sorted(dictionary.items(),                                                   
          key=operator.itemgetter(1),reverse=True)

令人惊讶的是,当我第一次运行程序时它运行良好,但是当我重新运行我的程序时它没有运行,它给了我以下错误:'list' object is not callable

【问题讨论】:

  • 你能定义什么是字典和运算符值吗?
  • @HeninRK dictionary={"math":10,"science":5,"english":20}
  • 我能够使用 Python 2.7.10 获得所需的输出
  • 您在某处为sorted...分配了一个值,即sorted = somevalue。在这条语句的上方,您可以执行print(sorted) 来查看它是否仍然是一个函数。
  • 使用 python 3.5 它正在工作。请提供完整的回溯,您确定它发生在sorted 期间吗?我想这可能是你事后做的手术。

标签: python sorting dictionary


【解决方案1】:
import operator

dictionary = {'math':10, 'science':5, 'english':20}

sorted_dictionary = sorted(dictionary.items(), key=operator.itemgetter(1), reverse=True)

print "Printing the key value pairs of dictionary after sorting on values of a dictionary {0}".format(sorted_dictionary)
print "Sorting the dictionary based on only the values: {0}".format(sorted(dictionary.values()))

输出--->

对字典的值排序后打印字典的键值对:[('english', 20), ('math', 10), ('science', 5)]

仅根据值对字典进行排序:[5, 10, 20]

【讨论】:

  • 您是说无法重现错误吗?那只是评论而不是答案。
  • @tdelaney - 是的,我无法重现该错误。代码 sn-p 似乎工作正常
  • @Sara Hamad - 你能实现你想要的吗?
猜你喜欢
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 2019-04-01
  • 2021-12-25
相关资源
最近更新 更多