对列表列表进行排序Python [英] Sort List of Lists of Lists Python
问题描述
我在python中有一个列表列表.我需要根据最低级别中的值之一对最高级别的列表进行排序.
I have a List of Lists of Lists in python. I need to sort the highest level of lists based on one of the values in the lowest levels.
对于玩具模型,我的列表首先创建为列表small
,该列表是一个包含三个项的列表:
For a toy model, my list is created first with list small
, which is a list containing lists with three items each:
small = [["dog", 5, 6], ["cat", 391, 130], ["pig", 45, 2]...]`
然后有一个较大的列表large
,该列表是通过将较小的列表和另一项附加在一起而创建的:
Then there is a larger list, large
, that is created by appending the small list and another item together:
large.append([["category 1"], small])`
这是在循环中完成的,因此,如果循环的计数器为5,它将通过创建列表small
的函数进行5次,每次将其添加到列表large
中.
This is done in a loop, so if the loop has a counter of 5, it will proceed through a function that creates a list small
5 times, each time, appending it to list large
.
我的目标是根据最小列表(包含small
列表的列表)中的元素对最终列表进行排序.我目前正在逐行打印large
列表,其中small
列表由其中的列表的index 1
排序(请参阅:顺序为5、10、15,顺序为1、3、6,等),所以我的输出是这样的:
My goal is to sort the final list based on an element in the smallest list (the list that comprises the small
list). I am currently printing the large
list out line by line, with the small
list ordered by the index 1
of the lists within it (see: 5,10,15 in order, 1,3,6 in order, etc), so my output is this:
>category 1:
>['Dog', 5, 6]
>['Pig', 10, 2]
>['Cat', 15, 130]
>['Buffalo', 20, 1]
>['Newt', 25, 45]
>category 2:
>['Newt', 1, 1092]
>['Cat', 3, 352]
>['Pig', 6, 34]
>['Buffalo', 9, 12]
>['Dog', 12, 5]
>category 3:
>['Buffalo', 2, 12]
>['Pig', 4, 37]
>['Cat', 6, 34]
>['Newt', 8, 150]
>['Dog', 10, 52]
但是,当我查看组成最小数组的列表中index 1
中的元素时,我想按最小到最大的顺序打印large
列表.所以我的理想输出是这样:
However, i would like to print the large
list in order of smallest to largest when looking at the element in index 1
in the list that makes up the smallest array. So my ideal output would be this:
>category 2:
>['Newt', 1, 1092]
>['Cat', 3, 352]
>['Pig', 6, 34]
>['Buffalo', 9, 12]
>['Dog', 12, 5]
>category 3:
>['Buffalo', 2, 12]
>['Pig', 4, 37]
>['Cat', 6, 34]
>['Newt', 8, 150]
>['Dog', 10, 52]
>category 1:
>['Dog', 5, 6]
>['Pig', 10, 2]
>['Cat', 15, 130]
>['Buffalo', 20, 1]
>['Newt', 25, 45]
我尝试用itemgetter()
进行排序,但是,当用itemgetter()
进行large
排序时,我只能对列表large
中的项目进行排序,这些项目是[category x]
和[[a,b,c], [a,b,c]]
.有没有办法访问itemgetter的最低级别的元素?或对列表进行排序的另一种方法?
I have tried sorting with itemgetter()
, however, when sorting large
with itemgetter()
, i can only sort the items in list large
, which are [category x]
and [[a,b,c], [a,b,c]]
. Is there a way to access the lowest level of elements for itemgetter? or another way to go about sorting this list?
谢谢.
推荐答案
这可以一行完成:
sorted_large = sorted(large, key=lambda item: item[1][0][1])
sorted
内置函数采用key
参数:
key
指定一个参数的函数,该参数用于提取一个 每个列表元素的比较键
key
specifies a function of one argument that is used to extract a comparison key from each list element
使用 lambda
表达式进行排序可以提取密钥.
Using a lambda
expression, the sorting key can be extracted.
这篇关于对列表列表进行排序Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!