2016-08-22 230 views
-1

在談到我剛纔的問題(解決): Only show certain number of emails in imaplib排序郵件按降序排列

所以,現在我可以告訴一定數量的我的收件箱的郵件,但我現在遇到的問題是,我想以降序輸出。

這裏說:http://pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/

>>> a[::-1] 
[8, 7, 6, 5, 4, 3, 2, 1] 

"And that -1 I snuck in at the end? It means to increment the index every time by -1, meaning it will traverse the list by going backwards."

所以即時通訊思想倒退等於降了吧? 我試圖加-1,我已經使用的代碼:

ids = data[0] 
id_list = ids.split() 

for num in id_list[0:10:-1]: 
    rv, data = M.fetch(num, '(RFC822)') 
    msg = email.message_from_string(data[0][1]) 

    subj = msg['Subject'] 
    to = msg['To'] 
    frm = msg['From'] 
    body = msg.get_payload() 

    print subj 

但沒有輸出。我是expecing

tenth 
ninth 
eigth 
. 
. 
. 
. 
second 
first 

但我沒有得到任何..有關如何實現我想要的輸出的任何幫助?

+0

在未排序的列表中,不向後不等於降序排序。如果要排序,則必須對列表進行排序。 –

+0

[我如何對Python中的字符串列表進行排序?](http://stackoverflow.com/questions/36139/how-do-i-sort-a-list-of-strings-in-python) –

回答

0

我要感謝馬克斯此解決方案,現在我的電子郵件被分類到最近

ids = data[0] 
id_list = ids.split() 

#[0:10] will only output 10 emails 
for num in id_list[10::-1]: 
    rv, data = M.fetch(num, '(RFC822)') 
    msg = email.message_from_string(data[0][1]) 

    subj = msg['Subject'] 
    to = msg['To'] 
    frm = msg['From'] 
    body = msg.get_payload() 

    print subj 

再次感謝你最大! :D