我的代码如下:
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
x[0]
,而不是x(0)
-后者试图像调用一个函数一样调用该元组。顺便说一下,元组是从左到右比较的,因此您可能根本不需要key
参数。