Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[Python]如何按asc顺序/desc顺序对列表嵌套元组排序?_Python_List_Sorting - Fatal编程技术网

[Python]如何按asc顺序/desc顺序对列表嵌套元组排序?

[Python]如何按asc顺序/desc顺序对列表嵌套元组排序?,python,list,sorting,Python,List,Sorting,首先,我不是以英语为母语的人。所以我的句子可能是有线的。请理解我 无论如何,当我做作业时,我必须按asc/desc顺序重新排列与元组嵌套的列表。 我的清单如下: lst=[(a,30),(b,80),(c,180),(d,200),(e,90),(f,1200),(g,120),(h,920),(i,7)] desc_lst=[(f,1200),(h,920),(d,200),(c,180),(g,120),(e,90),(b,80),(a,30),(i,7)] 我想列一张这样的清单: ls

首先,我不是以英语为母语的人。所以我的句子可能是有线的。请理解我

无论如何,当我做作业时,我必须按asc/desc顺序重新排列与元组嵌套的列表。 我的清单如下:

lst=[(a,30),(b,80),(c,180),(d,200),(e,90),(f,1200),(g,120),(h,920),(i,7)]
desc_lst=[(f,1200),(h,920),(d,200),(c,180),(g,120),(e,90),(b,80),(a,30),(i,7)]
我想列一张这样的清单:

lst=[(a,30),(b,80),(c,180),(d,200),(e,90),(f,1200),(g,120),(h,920),(i,7)]
desc_lst=[(f,1200),(h,920),(d,200),(c,180),(g,120),(e,90),(b,80),(a,30),(i,7)]

我该怎么分类呢?
我可以对普通列表进行排序,例如lst=[1,2,3,4,9,8,5]。但在本例中,我无法对这些进行排序,因为列表中有tuple作为元素。请回答。。。!我祝福你和你的家人

这样的方法应该行得通

lst = [('a', 30), ('b', 80), ('c', 180), ('d', 200), ('e', 90), ('f', 1200), ('g', 120), ('h', 920), ('i', 7)]
print(sorted(lst, key=lambda x: x[1], reverse=True))
输出结果为

[('f', 1200), ('h', 920), ('d', 200), ('c', 180), ('g', 120), ('e', 90), ('b', 80), ('a', 30), ('i', 7)]