TypeError:'tuple'对象在对元组中的项目进行排序时无法调用[关闭]

问题描述 投票:-2回答:1
我正在尝试编写一个程序来对元组列表(名称,年龄,分数)进行排序,其中名称是字符串,年龄和分数是数字。元组由控制台输入。排序标准是:

    根据名称排序
  1. 然后根据年龄排序
  2. 然后按分数排序
  • 优先级是姓名>年龄>得分。

    我的代码如下:

    def get_tuples(): lst=[] print ("enter the inputs: ") while True: ip =input() if ip: lst.append(tuple(ip.split(','))) else: break return lst ip=get_tuples() ip.sort(key=lambda x: (x(0),x(1),x(2))) print(ip)

    运行代码时出现以下错误:

    ip.sort(key=lambda x: (x(0),x(1),x(2))) TypeError: 'tuple' object is not callable

  • python sorting pycharm tuples
    1个回答
    0
    投票
    要从元组中获取元素,您需要x[0],而不是x(0)-后者试图像调用一个函数一样调用该元组。

    顺便说一下,元组是从左到右比较的,因此您可能根本不需要key参数。

    © www.soinside.com 2019 - 2024. All rights reserved.