在Python中对列表中的列表进行排序 [英] Sorting list-within-list in Python

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

问题描述

我在列表中有一个格式如下的列表:

I have a list within a list that is in this format:

[['39.9845450804', '-75.2089337931', 'Paved', ' ', 5.974380011873201],
['39.977111208', '-75.1326105278', 'Asphalt', '8', 3.4881723880136537],
['39.9767607107', '-75.1328155113', 'Asphalt', '20', 1.5123970222417986],
['40.089750884', '-74.9852538861', 'Paved', ' ', 4.296923142303416]]

其中的索引为latitudelongitudetype of pavementno. of empty spotslocation in KM from my current location

Where the indices are latitude, longitude, type of pavement, no. of empty spots, and location in KM from my current location

我想按我的第4个索引(location in KM from my current location)从最大到最小对这个未排序的列表进行排序.您可以假设列表中的列表将包含五个相同的数据点总数.

I want to sort this unsorted list from the greatest to lowest by my 4-th index, which is the location in KM from my current location. You can assume that the list-within-list will will contains the same total number of data points of five.

我知道将sort()用于一维列表,但是我不确定如何在列表中对列表进行排序.

I know to use sort() for single-dimensional lists, but I am not quite sure how to sort lists within lists.

我们将为您提供任何帮助.

Any assistance is appreciated.

推荐答案

如果my_data是您的列表,则按my_data中的第5个元素进行排序:

If my_data is your list then to sort by the 5th element in my_data you do:

sorted(my_data, key=lambda data: data[4])

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

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