Python 对嵌套列表中的特定项目组进行排序

Python 对嵌套列表中的特定项目组进行排序,python,Python,在这里,我在嵌套列表中有五个不同的列表。如何对最后一列(第5列)进行排序,而不是对整个嵌套列表进行排序?您的目的是: students = [["White", "Snow", 9, "F", 3.56], ["Sprat", "Jack", 12, "M", 2.0], ["Contrary", "Mary", 9, "F", 3.674], ["Dumpty", "Humpty", 11, "M", 2.342],

在这里,我在嵌套列表中有五个不同的列表。如何对最后一列(第5列)进行排序,而不是对整个嵌套列表进行排序?

您的目的是:

students = [["White", "Snow", 9, "F", 3.56],
            ["Sprat", "Jack", 12, "M", 2.0],
            ["Contrary", "Mary", 9, "F", 3.674],
            ["Dumpty", "Humpty", 11, "M", 2.342],
            ["Bunny", "Easter", 10, "M", 4.233],
            ["Wonderland", "Alice", 10, "F", 3.755],
            ["Bunyon", "Paul", 11, "M", 1.434],
            ["Penny", "Henny", 9, "F", 2.54],
            ["Hatter", "Mad", 11, "M", 4.522],
            ["Munk", "Chip", 10, "M", 3.0],
            ["Hood", "Red Riding", 10, "F", 3.137],
            ["Bunny", "Bugs", 11, "M", 2.12],
            ["Duck", "Daffy", 11, "M", 3.564],
            ["Ant", "Atom", 12, "M", 3.333],
            ["Mouse", "Mickey", 10, "M", 3.975],
            ["Brown", "Charlie", 9, "M", 1.25]]

您可以使用itemgetter

请尝试以下操作: 编辑:更改为在每行打印已排序列表的函数

#sort list by last element
sorted(students,key=lambda x:x[-1])
Out[155]: 
[['Brown', 'Charlie', 9, 'M', 1.25],
 ['Bunyon', 'Paul', 11, 'M', 1.434],
 ['Sprat', 'Jack', 12, 'M', 2.0],
 ['Bunny', 'Bugs', 11, 'M', 2.12],
 ['Dumpty', 'Humpty', 11, 'M', 2.342],
 ['Penny', 'Henny', 9, 'F', 2.54],
 ['Munk', 'Chip', 10, 'M', 3.0],
 ['Hood', 'Red Riding', 10, 'F', 3.137],
 ['Ant', 'Atom', 12, 'M', 3.333],
 ['White', 'Snow', 9, 'F', 3.56],
 ['Duck', 'Daffy', 11, 'M', 3.564],
 ['Contrary', 'Mary', 9, 'F', 3.674],
 ['Wonderland', 'Alice', 10, 'F', 3.755],
 ['Mouse', 'Mickey', 10, 'M', 3.975],
 ['Bunny', 'Easter', 10, 'M', 4.233],
 ['Hatter', 'Mad', 11, 'M', 4.522]]
结果应该如下所示: 该函数根据每个列表最后一个值中的数字进行排序

from operator import itemgetter

students = [["White", "Snow", 9, "F", 3.56],
            ["Sprat", "Jack", 12, "M", 2.0],
            ["Contrary", "Mary", 9, "F", 3.674],
            ["Dumpty", "Humpty", 11, "M", 2.342],
            ["Bunny", "Easter", 10, "M", 4.233],
            ["Wonderland", "Alice", 10, "F", 3.755],
            ["Bunyon", "Paul", 11, "M", 1.434],
            ["Penny", "Henny", 9, "F", 2.54],
            ["Hatter", "Mad", 11, "M", 4.522],
            ["Munk", "Chip", 10, "M", 3.0],
            ["Hood", "Red Riding", 10, "F", 3.137],
            ["Bunny", "Bugs", 11, "M", 2.12],
            ["Duck", "Daffy", 11, "M", 3.564],
            ["Ant", "Atom", 12, "M", 3.333],
            ["Mouse", "Mickey", 10, "M", 3.975],
            ["Brown", "Charlie", 9, "M", 1.25]]

def printSortedStudents():
    sortedStudents = sorted(students, key=itemgetter(4))
    # You can change the index value of itemgetter(4) to define what index to sort with.
    for l in sortedStudents:
        print (l)

printSortedStudents()

您希望的输出是什么?从最小到最大排序。请参见下面的我的答案。Whey
Raleigh Clemens
!欢迎来到stackoverflow。当你问一个问题时,一定要表明你在迄今为止所做的努力。如果你至少没有尝试自己解决一个问题,大多数人都不会费心去回答。如果您得到的答案确实解决了您的问题(请参阅下面的答案:D),请确保您通过单击答案旁边的复选标记进行投票并接受答案。当我键入排序(students,key=lambda x:x[-1])以输入students:a、b、c、D时,e=条目打印(a、b、c、d、e)列表的最后一列仍然没有排序。是。您是如何到达的?key=lambda x:x[-1]告诉排序函数根据内部列表中的最后一个元素对外部列表进行排序。
['Brown', 'Charlie', 9, 'M', 1.25]
['Bunyon', 'Paul', 11, 'M', 1.434]
['Sprat', 'Jack', 12, 'M', 2.0]
['Bunny', 'Bugs', 11, 'M', 2.12]
['Dumpty', 'Humpty', 11, 'M', 2.342]
['Penny', 'Henny', 9, 'F', 2.54]
['Munk', 'Chip', 10, 'M', 3.0]
['Hood', 'Red Riding', 10, 'F', 3.137]
['Ant', 'Atom', 12, 'M', 3.333]
['White', 'Snow', 9, 'F', 3.56]
['Duck', 'Daffy', 11, 'M', 3.564]
['Contrary', 'Mary', 9, 'F', 3.674]
['Wonderland', 'Alice', 10, 'F', 3.755]
['Mouse', 'Mickey', 10, 'M', 3.975]
['Bunny', 'Easter', 10, 'M', 4.233]
['Hatter', 'Mad', 11, 'M', 4.522]