[英]How do I make a function that returns a list of tuples sorted by the last element in the tuple?
This is the code I am trying to run but it doesn't work. 这是我尝试运行的代码,但是它不起作用。 It says invalid syntax. 它说无效的语法。
def sort_last(tuples):
return sorted(tuples, key = tuples[-1]
x = [(7, 6), (5, 5), (2, 1)]
print sort_last(x)
Use a lambda: 使用lambda:
def sort_last(tuples):
return sorted(tuples, key = lambda t: t[-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.