[英]How do I make a function that returns a list of tuples sorted by the last element in the tuple?
這是我嘗試運行的代碼,但是它不起作用。 它說無效的語法。
def sort_last(tuples):
return sorted(tuples, key = tuples[-1]
x = [(7, 6), (5, 5), (2, 1)]
print sort_last(x)
使用lambda:
def sort_last(tuples):
return sorted(tuples, key = lambda t: t[-1])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.