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

Python 按时间跨度对该元组列表进行排序,python,list,sorting,lambda,timestamp,Python,List,Sorting,Lambda,Timestamp,此lambda函数有什么问题: 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']) 我得到以下错误: content2_dict = sorted(content_dict, key=lambda x: datetime.strptime(x[0

此lambda函数有什么问题:

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'])
我得到以下错误:

content2_dict =  sorted(content_dict, key=lambda x: datetime.strptime(x[0],'%a, %d %b %H:%M:%S %z'))
回溯(最近一次呼叫最后一次):
文件“/Users/amirnakhostin/Documents/Computer Science/Python/test.py”,第17行,在
content2\u dict=sorted(content\u dict,key=lambda x:datetime.strtime(x[1],'%a,%d%b%H:%M:%S'))
文件“/Users/amirnakhostin/Documents/Computer Science/Python/test.py”,第17行,在
content2\u dict=sorted(content\u dict,key=lambda x:datetime.strtime(x[1],'%a,%d%b%H:%M:%S'))
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py”,第325行,在_strptime中
(数据字符串,格式))
ValueError:时间数据“o”与格式“%a,%d%b%H:%M:%S”不匹配

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

要访问
目录中的时间字符串
,应使用
x[1][0]
而不是
x[0]

这里有一个变通解决方案,因为python(或datetime模块)在处理时区时遇到了一些问题(HKT可以解析,而EDT不能解析,这让我很困惑),并且假设所有时区都是相同的,我只是简单地将所有时区剥离掉

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'

有几个问题,您在lambda中索引了错误的内容,您忘记了一年,
%H:%M:%S
可以写成
%X
,而
EDT
,不幸的是,无法解析:

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

您的代码有几个问题。由于省略了年份
%Y
,您的格式不正确。您还将日期附在一个列表中,这会导致其他问题。当您的日期包含在
x[1]
中时,您正试图使用
x[0]
。下面是一个(几乎所有)代码的工作示例(请注意,时区已被删除。有关原因,请参见下文)

然而,我们从中了解到,
strtime
在时区方面存在一些问题。为了解决这个问题,我们使用包

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

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

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

我也有一个时区的问题,必须删除它。


如何删除它?如何在for循环中使用dateutil来解析时间戳?使用
parser.parse()
返回一个datetime对象。按返回的文件排序object@AmirNakhostin我已经编辑了我的答案以回答您的评论。这仅在我有3个值要分析时有效,但内容[]是200篇文章的列表。那么,是不是在循环中使用解析器来解析时区,并创建一个包含标题和新时间的新元组呢?谢谢,我以为是这样,但还是出错了。我认为是原始时间戳的格式导致了这一问题。
>>> 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')]
>>> 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))
>>> 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)))]
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