对列表列表进行排序的方法? [英] Method to sort a list of lists?

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

问题描述

我有一个列表列表(因为我必须动态生成它,所以不能是元组),它的结构为一个int和一个float的列表列表,就像这样:

I have a list of lists (can't be tuples since I have to generate it dynamically) and it is structured as a list of lists of one int and one float Like so:

[[1,1.0345],[2,5.098],[3,4.89],[2,5.97]]

我想对它进行排序,但是我只能设法获得内置的排序功能,以便按列表的第一个元素对其进行排序,或者什么也不做,但是我需要按列表的第二个元素对它们进行排序,然后我不想实现自己的排序功能.因此,我想要的一个示例是:

I want to get it sorted but I have only managed to get the built in sorting function to sort it by the first element of the lists or not do anything, but I need to sort them by the second element of the list and I don't want to implement my own sorting function. So an example of what I would want is:

[[1,1.0345],[3,4.89],[2,5.098],[2,5.97]]

有人可以告诉我如何获得内置的排序功能之一吗?

Could someone tell me how to get one of the built in sorting functions to do this?

推荐答案

传递key参数.

L.sort(key=operator.itemgetter(1))

这篇关于对列表列表进行排序的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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