Python sort()列表的第一个元素 [英] Python sort() first element of list

查看:304
本文介绍了Python sort()列表的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含非特定数量元素的列表,但是嵌套列表的每个第一个元素都是一个标识符,我想使用该标识符对列表进行排序

I have a list that contains non specific amount of elements but every first element of the nested list is an identifier, I would like to use that identifier to sort the list in order

list = [['D', 'F', 'E', 'D', 'F', 'D'],['A', 'F', 'E', 'C', 'F', 'E'],['C', 'E', 'E', 'F', 'E', 'E'],['B', 'F', 'E', 'D', 'F', 'F']]

排序后

list = [['A', 'F', 'E', 'C', 'F', 'E'],['B', 'F', 'E', 'D', 'F', 'F'],['C', 'E', 'E', 'F', 'E', 'E'],['D', 'F', 'E', 'D', 'F', 'D']]

我正在使用python 3.3.3

I am using python 3.3.3

推荐答案

Python自动根据第一个元素对列表列表进行排序.例如:

Python automatically sorts lists of lists by the first element. For example:

lol=[[1,2,3],[5,6,7],[0,9,9]]
sorted(lol)
[[0, 9, 9], [1, 2, 3], [5, 6, 7]]

这篇关于Python sort()列表的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆