排序邮件按降序排列

问题描述:

在谈到我刚才的问题(解决): 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://*.com/questions/36139/how-do-i-sort-a-list-of-strings-in-python) –

我要感谢马克斯此解决方案,现在我的电子邮件被分类到最近

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