list by append函数不能按sort函数排序

时间:2016-07-24 03:43:36

标签: python list sorting

我使用append函数创建一个带循环的列表来填充列表。 之后我想在其中一个元素的属性上使用列表排序函数对其进行排序。但它不起作用,你们可以帮助我,非常感谢。 这是代码

def processRawUrlData():
    rawData = readHtml()
    taskList = []
    for item in rawData:
        if item != '':
            taskList.append(processTask(item))
    taskList.sort(key=attrgetter('est_time'))
    for item in taskList:
        print(item.taskname)
        print(item.est_time)
        print(item.submittedDate)
    return taskList

1 个答案:

答案 0 :(得分:0)

我认为您可能必须更具体并提供比较功能,以便您的列表可以进行排序。从以下网站获取以下代码。希望它有所帮助:

https://wiki.python.org/moin/HowTo/Sorting

    >>> student_tuples = [
            ('john', 'A', 15),
            ('jane', 'B', 12),
            ('dave', 'B', 10),
    ]
    >>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
    [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]