如何使用日期对Python列表排序

如何使用日期对Python列表排序,python,python-3.x,Python,Python 3.x,我在Python中有这样一个列表 myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12'] 我想使用键中的sorted和lambda按日期对该列表进行排序 Ex: myList = ['http://google.com Google 2018-07-10', 'http://appl

我在Python中有这样一个列表

myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

我想使用
键中的
sorted
lambda
按日期对该列表进行排序

Ex:

myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

print( sorted(myList, key= lambda x: x.split()[-1], reverse=True) )
print( sorted(myList, key= lambda x: x.split()[-1]) )
['http://microsoft.com Microsoft 2018-07-12', 'http://apple.com Apple Inc 2018-07-11', 'http://google.com Google 2018-07-10']
['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']
输出:

myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

print( sorted(myList, key= lambda x: x.split()[-1], reverse=True) )
print( sorted(myList, key= lambda x: x.split()[-1]) )
['http://microsoft.com Microsoft 2018-07-12', 'http://apple.com Apple Inc 2018-07-11', 'http://google.com Google 2018-07-10']
['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

您可以拆分每个字符串,取最后一部分,然后按此部分排序:

myList = [
    'http://apple.com Apple Inc 2018-07-11', 
    'http://google.com Google 2018-07-10',     
    'http://microsoft.com Microsoft 2018-07-12'
]

sorted(myList, key=lambda s: s.split()[-1])
输出:

['http://google.com Google 2018-07-10',
 'http://apple.com Apple Inc 2018-07-11',
 'http://microsoft.com Microsoft 2018-07-12']

您还可以通过应用
键对列表进行排序

>>> from datetime import datetime
>>> myList = ['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']
>>> sorted(myList, key=lambda x: datetime.strptime(x.split()[-1], '%Y-%m-%d'))
['http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

注意:这可能会稍微复杂一些,因为ISO对日期进行了格式化,并对字符串日期进行了完美的排序,如其他答案所示。使用
strtime()
只是为了确保日期按正确的日期格式排序

在更一般的情况下,这里有一种方法应该有效:

from dateutil.parser import parse

myList = [
    'http://google.com Google 2018-07-10',
    'http://apple.com Apple Inc 2018-07-11',
    'Foo 2017-07-13 http://whatever.com',
    'http://microsoft.com Microsoft 2018-07-12',
    '2015-07-15 http://whatever.com Whatever'
]

dct = {parse(v, fuzzy=True): v for v in myList}
print([dct[k] for k in sorted(dct, reverse=True)])
print([dct[k] for k in sorted(dct)])
这样,您就不会被迫在列表字符串的末尾添加日期,输出:

['http://microsoft.com Microsoft 2018-07-12', 'http://apple.com Apple Inc 2018-07-11', 'http://google.com Google 2018-07-10', 'Foo 2017-07-13 http://whatever.com', '2015-07-15 http://whatever.com Whatever']
['2015-07-15 http://whatever.com Whatever', 'Foo 2017-07-13 http://whatever.com', 'http://google.com Google 2018-07-10', 'http://apple.com Apple Inc 2018-07-11', 'http://microsoft.com Microsoft 2018-07-12']

你试过什么吗?是的,我试过在添加之前对字典进行排序,但是我的字典的键值总是在变化,所以我无法用listthank解决它。谢谢!但你能解释一下python是如何处理字典中的随机列表数据的吗?