2017-04-24 42 views
0
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),但不是整个嵌套列表?对嵌套列表中的一组特定项目进行排序

+3

你想要的输出是什么? – Allen

+0

从最小到最大排序。 –

+0

在下面看到我的回答 – Allen

回答

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

是的。你是怎么到达那里的? –

+0

key = lambda x:x [-1]告诉排序函数根据内部列表中的最后一个元素对外部列表进行排序。 – Allen

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]