在Python中对列表中的列表进行排序

编程入门 行业动态 更新时间:2024-10-09 13:25:58
本文介绍了在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]]

其中的索引为latitude,longitude,type of pavement,no. of empty spots和location 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中对列表中的列表进行排序

本文发布于:2023-11-29 09:02:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中对   列表   列表中   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!