Python-如何在列表列表中更改列表顺序

时间:2015-11-21 18:20:26

标签: python-2.7

我有一份列表清单,我希望按照一个人离开工作的时间排序。我已经制作了一个代码,只给我时间,但我需要检查时间并按顺序排列。

inFile=removeHeader(file_name) # the information is taken from a txt file and this only gives me the part of the services
#print inFile gives me list of lists like [['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']

for time in inFile:
    hours=time[4]

    print hours  #this gives me only the hours they left work

1 个答案:

答案 0 :(得分:-1)

请参阅https://docs.python.org/2/library/functions.html#sorted

您必须按照休假时间排序主列表,即子列表的第5个值,排序的关键参数允许您指定一个函数,该函数将被调用以计算列表的排序基于:

import time
x =[['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']]
sorted(x, key = lambda x: x[4])
相关问题