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

2024-10-01 02:18:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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]]

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


Tags: 列表whitejackalicemarybunnysnoweaster
2条回答

这就是你想要的:

#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]]

你可以使用itemgetter

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

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()

结果应该是这样的: 函数按每个列表最后一个值中的数字排序

['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]

相关问题 更多 >