2016-07-24 34 views
-6

我使用append函數創建一個帶有循環的列表來填充列表。 並且我想在列表排序函數中對其中一個元素的屬性進行排序。但它不起作用,你們能幫助我嗎,非常感謝。 這裏是代碼list by 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

你說的 「它不工作」 是什麼意思?你有例外嗎?你的列表是不是完全排序,還是排序,但不是你打算的方式?我們無法猜測這些東西!您可能還想顯示'processTask'返回值的'est_time'屬性的樣子,因爲這就是您想要排序的內容。 – Blckknght

+1

請給出[mcve] – Julien

+0

尋求調試幫助的問題(**「爲什麼這個代碼不工作?」**)必須包含所需的行爲,*特定的問題或錯誤*和*必需的最短代碼*它**在問題本身**。沒有**明確問題陳述**的問題對其他讀者沒有用處。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – MattDMo

回答

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