[英]Reversing the order of a list of lists or list of tuples
我有一個元組列表:
ls = [('hello', 'there'), ('whats', 'up'), ('no', 'idea')]
我想顛倒列表中每個元組的順序。
ls = [('there', 'hello'), ('up', 'whats'), ('idea', 'no')]
我知道元組是不可變的,所以我需要創建新的元組。 我不太確定 go 的最佳方法是什么。 我可以將元組列表更改為列表列表,但我認為 go 可能有更有效的方法。
輸入:
ls = [('hello', 'there'), ('whats', 'up'), ('no', 'idea')]
ls = [(f,s) for s,f in ls]
print(ls)
Output:
[('there', 'hello'), ('up', 'whats'), ('idea', 'no')]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.