根据特定元素对列表进行排序-Python [英] Sorting lists based on a particular element - Python

查看:136
本文介绍了根据特定元素对列表进行排序-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据Python中列表的第一个元素对列表进行排序?

How do I sort a list of lists based on the first element of the lists in Python?

>>> list01 = (['a','b','c'],['b','a','d'],['d','e','c'],['a','f','d'])
>>> map(sorted, list01)
[['a', 'b', 'c'], ['a', 'b', 'd'], ['c', 'd', 'e'], ['a', 'd', 'f']]
>>> sorted(map(sorted, list01))
[['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'd', 'f'], ['c', 'd', 'e']]

推荐答案

from operator import itemgetter    
sorted(list01, key=itemgetter(0))

这篇关于根据特定元素对列表进行排序-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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