簡體   English   中英

如何制作一個返回元組列表的函數,該列表按元組中的最后一個元素排序?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM