通过H:M:S格式下降时间的顺序列表在Python中

时间:2017-11-05 16:01:13

标签: python python-3.x python-3.5

我正在尝试订购一个类似于此的列表:

  

['2017-11-03 15:21:00','2017-11-03 15:03:00','2017-11-03 15:11:00',   '2017-11-03 15:52:00','2017-11-03 14:37:00','2017-11-03 14:32:00',   '2017-11-03 15:20:00','2017-11-03 14:30:00','2017-11-03 14:43:00',   '2017-11-03 14:58:00','2017-11-03 15:58:00','2017-11-03 15:42:00',   '2017-11-03 14:59:00','2017-11-03 15:40:00','2017-11-03 14:35:00',   '2017-11-03 14:21:00','2017-11-03 15:26:00','2017-11-03 15:50:00',   '2017-11-03 15:13:00','2017-11-03 15:53:00','2017-11-03 14:40:00',   '2017-11-03 15:16:00','2017-11-03 14:45:00','2017-11-03 15:33:00',   '2017-11-03 15:35:00','2017-11-03 15:36:00','2017-11-03 14:23:00',   '2017-11-03 15:10:00','2017-11-03 15:37:00','2017-11-03 14:54:00',   '2017-11-03 15:44:00','2017-11-03 15:23:00','2017-11-03 14:47:00',   '2017-11-03 15:43:00','2017-11-03 14:48:00','2017-11-03 14:41:00',   '2017-11-03 15:45:00','2017-11-03 15:09:00','2017-11-03 15:06:00',   '2017-11-03 15:08:00','2017-11-03 14:57:00','2017-11-03 15:17:00',   '2017-11-03 14:52:00','2017-11-03 15:14:00','2017-11-03 15:19:00',   '2017-11-03 15:25:00','2017-11-03 15:48:00','2017-11-03 15:24:00',   '2017-11-03 15:57:00','2017-11-03 15:28:00','2017-11-03 15:34:00',   '2017-11-03 15:59:00','2017-11-03 14:53:00','2017-11-03 14:50:00',   '2017-11-03 14:55:00','2017-11-03 15:56:00','2017-11-03 15:46:00',   '2017-11-03 15:32:00','2017-11-03 16:00:00','2017-11-03 15:29:00',   '2017-11-03 14:34:00','2017-11-03 15:04:00','2017-11-03 14:38:00',   '2017-11-03 15:07:00','2017-11-03 14:39:00','2017-11-03 14:25:00',   '2017-11-03 14:51:00','2017-11-03 14:33:00','2017-11-03 14:46:00',   '2017-11-03 14:22:00','2017-11-03 15:47:00','2017-11-03 14:42:00',   '2017-11-03 15:27:00','2017-11-03 15:41:00','2017-11-03 15:55:00',   '2017-11-03 15:31:00','2017-11-03 14:56:00','2017-11-03 14:49:00',   '2017-11-03 15:39:00','2017-11-03 15:01:00','2017-11-03 14:29:00',   '2017-11-03 14:27:00','2017-11-03 14:44:00','2017-11-03 15:49:00',   '2017-11-03 15:30:00','2017-11-03 15:51:00','2017-11-03 15:54:00',   '2017-11-03 15:22:00','2017-11-03 14:31:00','2017-11-03 14:28:00',   '2017-11-03 14:26:00','2017-11-03 15:15:00','2017-11-03 14:24:00',   '2017-11-03 15:00:00','2017-11-03 15:38:00','2017-11-03 14:36:00',   '2017-11-03 15:18:00','2017-11-03 15:05:00','2017-11-03 15:12:00',   '2017-11-03 15:02:00']

看起来像:

  

['2017-11-03 16:00:00','2017-11-03 15:59:00','2017-11-03 15:58:00'等...

我想知道如何实现这一点,但我也有兴趣知道如何通过将它们按顺序放入列表中来预先解决问题。

我正在从https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo

的json创建列表

我只是将每个项目添加到这样的列表中:

times = []
if (interval == '1min'):
   for item in data['Time Series (1min)']:
       times.append(item)

但是我得到了更早的列表并且它没有按顺序排列,即使你访问网页,时间也是按降序排列。

感谢所有帮助!

1 个答案:

答案 0 :(得分:1)

字符串未正确排序,因为它们来自JSON字典。当JSON转换为Python对象时,字符串落在Python字典中,该字典未排序。

您只需要sorted

>>> sorted(times, reverse=True)
['2017-11-03 16:00:00', '2017-11-03 15:59:00', '2017-11-03 15:58:00', '2017-11-03 15:57:00', '2017-11-03 15:56:00', '2017-11-03 15:55:00', '2017-11-03 15:54:00', '2017-11-03 15:53:00', '2017-11-03 15:52:00', '2017-11-03 15:51:00', '2017-11-03 15:50:00', '2017-11-03 15:49:00', '2017-11-03 15:48:00', '2017-11-03 15:47:00', '2017-11-03 15:46:00', '2017-11-03 15:45:00', '2017-11-03 15:44:00', '2017-11-03 15:43:00', '2017-11-03 15:42:00', '2017-11-03 15:41:00', '2017-11-03 15:40:00', '2017-11-03 15:39:00', '2017-11-03 15:38:00', '2017-11-03 15:37:00', '2017-11-03 15:36:00', '2017-11-03 15:35:00', '2017-11-03 15:34:00', '2017-11-03 15:33:00', '2017-11-03 15:32:00', '2017-11-03 15:31:00', '2017-11-03 15:30:00', '2017-11-03 15:29:00', '2017-11-03 15:28:00', '2017-11-03 15:27:00', '2017-11-03 15:26:00', '2017-11-03 15:25:00', '2017-11-03 15:24:00', '2017-11-03 15:23:00', '2017-11-03 15:22:00', '2017-11-03 15:21:00', '2017-11-03 15:20:00', '2017-11-03 15:19:00', '2017-11-03 15:18:00', '2017-11-03 15:17:00', '2017-11-03 15:16:00', '2017-11-03 15:15:00', '2017-11-03 15:14:00', '2017-11-03 15:13:00', '2017-11-03 15:12:00', '2017-11-03 15:11:00', '2017-11-03 15:10:00', '2017-11-03 15:09:00', '2017-11-03 15:08:00', '2017-11-03 15:07:00', '2017-11-03 15:06:00', '2017-11-03 15:05:00', '2017-11-03 15:04:00', '2017-11-03 15:03:00', '2017-11-03 15:02:00', '2017-11-03 15:01:00', '2017-11-03 15:00:00', '2017-11-03 14:59:00', '2017-11-03 14:58:00', '2017-11-03 14:57:00', '2017-11-03 14:56:00', '2017-11-03 14:55:00', '2017-11-03 14:54:00', '2017-11-03 14:53:00', '2017-11-03 14:52:00', '2017-11-03 14:51:00', '2017-11-03 14:50:00', '2017-11-03 14:49:00', '2017-11-03 14:48:00', '2017-11-03 14:47:00', '2017-11-03 14:46:00', '2017-11-03 14:45:00', '2017-11-03 14:44:00', '2017-11-03 14:43:00', '2017-11-03 14:42:00', '2017-11-03 14:41:00', '2017-11-03 14:40:00', '2017-11-03 14:39:00', '2017-11-03 14:38:00', '2017-11-03 14:37:00', '2017-11-03 14:36:00', '2017-11-03 14:35:00', '2017-11-03 14:34:00', '2017-11-03 14:33:00', '2017-11-03 14:32:00', '2017-11-03 14:31:00', '2017-11-03 14:30:00', '2017-11-03 14:29:00', '2017-11-03 14:28:00', '2017-11-03 14:27:00', '2017-11-03 14:26:00', '2017-11-03 14:25:00', '2017-11-03 14:24:00', '2017-11-03 14:23:00', '2017-11-03 14:22:00', '2017-11-03 14:21:00']

格式很方便,因为按字典顺序对字符串进行排序会按时间顺序排序。