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

时间:2017-04-24 14:04:01

标签: 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列)而不是整个嵌套列表?

2 个答案:

答案 0 :(得分:0)

这就是你的目标:

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

答案 1 :(得分:0)

您可以使用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]