对包含元组的元组进行排序 [英] Sorting a tuple that contains tuples
本文介绍了对包含元组的元组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下元组,其中包含元组:
I have the following tuple, which contains tuples:
MY_TUPLE = (
('A','Apple'),
('C','Carrot'),
('B','Banana'),
)
我想根据内部元组中包含的 second 值对这个元组进行排序(即,排序 Apple、Carrot、Banana 而不是 A、B、C).
I'd like to sort this tuple based upon the second value contained in inner-tuples (i.e., sort Apple, Carrot, Banana rather than A, B, C).
有什么想法吗?
推荐答案
from operator import itemgetter
MY_SORTED_TUPLE = tuple(sorted(MY_TUPLE, key=itemgetter(1)))
或没有itemgetter
:
MY_SORTED_TUPLE = tuple(sorted(MY_TUPLE, key=lambda item: item[1]))
这篇关于对包含元组的元组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文