按时间跨度对此元组列表进行排序

时间:2014-06-01 05:10:48

标签: python list sorting lambda timestamp

content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32 EDT']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44 EDT'])

这个lambda函数有什么问题:

content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[0],'%a, %d %b %H:%M:%S %z'))

我收到以下错误:

Traceback (most recent call last):
  File "/Users/amirnakhostin/Documents/Computer Science /Python/test.py", line 17, in <module>
    content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[1],'%a, %d %b %H:%M:%S'))
  File "/Users/amirnakhostin/Documents/Computer Science /Python/test.py", line 17, in <lambda>
    content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[1],'%a, %d %b %H:%M:%S'))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'o' does not match format '%a, %d %b %H:%M:%S'

4 个答案:

答案 0 :(得分:1)

我也遇到了时区问题,不得不将其删除。

import datetime

content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44'])]

content2_dict =  sorted(content_dict, key=lambda x: datetime.datetime.strptime(x[1][0],'%a, %d %b %Y %H:%M:%S'))
print content2_dict

答案 1 :(得分:0)

%Y%b之间缺少%H:%M:%S

要访问content_dict中的时间字符串,应使用x[1][0]代替x[0]

这是一个解决方案,因为python(或datetime模块)有一些问题处理时区(可以解析HKT而EDT可以让我感到困惑)并假设所有时区都相同,我只是简单地剥夺了所有时区。

content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[1][0][:-4], '%a, %d %b %Y %H:%M:%S'))

答案 2 :(得分:0)

有几个问题,你在lambda中索引错误的东西,你忘记了一年,%H:%M:%S可以写成%X,而EDT,不幸的是,可以&# 39;解析:

from datetime import datetime
content_dict = [(u'Bowe Bergdahl', [u'Sat, 31 May 2014 16:03:32']), (u"U.S. 'hypocrisy' in cybertheft charge", [u'Fri, 23 May 2014 02:30:44'])]
content_dict_ = sorted(content_dict, key=lambda x: datetime.strptime(x[1][0],'%a, %d %B %Y %X'))

答案 3 :(得分:0)

您的代码存在一些问题。您通过省略年strptime来格式化%Y。您还将日期括在列表中,这会导致其他问题。当您的日期被x[0]括起来时,您试图使用x[1]。以下是(几乎所有)代码的工作示例(请注意,时区已被删除。请参阅下面的原因)。

>>> content = [(u'Bowe Bergdahl',u'Sat, 31 May 2014 16:03:32'), (u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44')]
>>> content2 = sorted(content, key=lambda x:datetime.datetime.strptime(x[1], '%a, %d %B %Y %H:%M:%S'))
>>> content2
[(u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44'), (u'Bowe Bergdahl', u'Sat, 31 May 2014 16:03:32')]

但是我们从this question得到strptime有时区问题。要解决该问题,我们使用dateutil

>>> from dateutil import parser
>>> parser.parse(u'Sat, 31 May 2014 16:03:32 -0400')
datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400))

请注意,-0400是相对于GMT的EDT。

要使用此功能对列表进行排序,请执行以下操作

>>> from dateutil import parser
>>> unformatted_content = [(u'Bowe Bergdahl', u'Sat, 31 May 2014 16:03:32 -0400'), (u"U.S. 'hypocrisy' in cybertheft charge", u'Fri, 23 May 2014 02:30:44 -0400')]
>>> real_content = [(item[0], parser.parse(item[1])) for item in unformatted_content]
>>> real_content
[(u'Bowe Bergdahl', datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400))), (u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44, tzinfo=tzoffset(None, -14400)))]
>>> content2 = sorted(real_content, key=lambda x:x[1])
>>> content2
[(u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44, tzinfo=tzoffset(None, -14400))), (u'Bowe Bergdahl', datetime.datetime(2014, 5, 31, 16, 3, 32, tzinfo=tzoffset(None, -14400)))]