data = ['2023-05-10 20:37:49', '2023-05-10 20:37:50', '2023-05-10 20:37:51', '2023-05-10 20:37:52', '2023-05-10 20:37:53', '2023-05-10 20:37:54', '2023-05-10 20:37:55', '2023-05-10 20:37:56', '2023-05-10 20:37:57', '2023-05-10 20:37:58', '2023-05-10 20:37:59', '2023-05-10 20:38:00', '2023-05-10 20:38:01', '2023-05-10 20:38:02', '2023-05-10 20:38:03', '2023-05-10 20:38:04', '2023-05-10 20:38:05', '2023-05-10 20:38:06', '2023-05-10 20:38:07', '2023-05-10 20:38:08', '2023-05-10 20:38:09', '2023-05-10 20:38:10', '2023-05-10 20:38:11', '2023-05-10 20:38:12', '2023-05-10 20:38:13', '2023-05-10 20:38:14', '2023-05-10 20:38:15', '2023-05-10 20:38:16', '2023-05-10 20:38:17', '2023-05-10 20:38:18', '2023-05-10 20:38:19', '2023-05-10 20:38:20', '2023-05-10 20:38:21', '2023-05-10 20:38:22', '2023-05-10 20:38:23', '2023-05-10 20:38:24', '2023-05-10 20:38:25', '2023-05-10 20:38:26', '2023-05-10 20:38:27', '2023-05-10 20:38:28', '2023-05-10 20:59:25', '2023-05-10 20:59:26', '2023-05-10 20:59:27', '2023-05-10 20:59:28', '2023-05-10 20:59:29', '2023-05-10 20:59:30', '2023-05-10 20:59:31', '2023-05-10 20:59:32', '2023-05-10 20:59:33', '2023-05-10 20:59:34', '2023-05-10 20:59:35', '2023-05-10 20:59:36', '2023-05-10 20:59:37', '2023-05-10 20:59:38', '2023-05-10 20:59:39', '2023-05-10 20:59:40', '2023-05-10 20:59:41', '2023-05-10 20:59:42', '2023-05-10 20:59:43', '2023-05-10 20:59:44', '2023-05-10 20:59:45', '2023-05-10 20:59:46', '2023-05-10 20:59:47', '2023-05-10 20:59:48', '2023-05-10 20:59:49', '2023-05-10 20:59:50', '2023-05-10 20:59:51', '2023-05-10 20:59:52', '2023-05-10 20:59:53', '2023-05-10 20:59:54', '2023-05-10 20:59:55', '2023-05-10 20:59:56', '2023-05-10 20:59:57', '2023-05-10 20:59:58', '2023-05-10 20:59:59', '2023-05-10 21:00:00'] 在data里面我想筛选出2023-05-09 18:04:13到2023-05-09 23:47:24之前的数据也包括2023-05-09 18:04:13和2023-05-09 23:47:24该怎么做
时间: 2023-12-10 19:42:26 浏览: 94
HC-05 AT指令集
你的数据列表中的时间格式是字符串,我们需要将其转换为datetime格式才能比较大小。可以使用datetime.strptime()函数进行转换。然后,我们可以使用比较运算符(<和>)来筛选出符合条件的时间。
下面是代码示例:
```python
from datetime import datetime
data = ['2023-05-10 20:37:49', '2023-05-10 20:37:50', '2023-05-10 20:37:51', '2023-05-10 20:37:52', '2023-05-10 20:37:53', '2023-05-10 20:37:54', '2023-05-10 20:37:55', '2023-05-10 20:37:56', '2023-05-10 20:37:57', '2023-05-10 20:37:58', '2023-05-10 20:37:59', '2023-05-10 20:38:00', '2023-05-10 20:38:01', '2023-05-10 20:38:02', '2023-05-10 20:38:03', '2023-05-10 20:38:04', '2023-05-10 20:38:05', '2023-05-10 20:38:06', '2023-05-10 20:38:07', '2023-05-10 20:38:08', '2023-05-10 20:38:09', '2023-05-10 20:38:10', '2023-05-10 20:38:11', '2023-05-10 20:38:12', '2023-05-10 20:38:13', '2023-05-10 20:38:14', '2023-05-10 20:38:15', '2023-05-10 20:38:16', '2023-05-10 20:38:17', '2023-05-10 20:38:18', '2023-05-10 20:38:19', '2023-05-10 20:38:20', '2023-05-10 20:38:21', '2023-05-10 20:38:22', '2023-05-10 20:38:23', '2023-05-10 20:38:24', '2023-05-10 20:38:25', '2023-05-10 20:38:26', '2023-05-10 20:38:27', '2023-05-10 20:38:28', '2023-05-10 20:59:25', '2023-05-10 20:59:26', '2023-05-10 20:59:27', '2023-05-10 20:59:28', '2023-05-10 20:59:29', '2023-05-10 20:59:30', '2023-05-10 20:59:31', '2023-05-10 20:59:32', '2023-05-10 20:59:33', '2023-05-10 20:59:34', '2023-05-10 20:59:35', '2023-05-10 20:59:36', '2023-05-10 20:59:37', '2023-05-10 20:59:38', '2023-05-10 20:59:39', '2023-05-10 20:59:40', '2023-05-10 20:59:41', '2023-05-10 20:59:42', '2023-05-10 20:59:43', '2023-05-10 20:59:44', '2023-05-10 20:59:45', '2023-05-10 20:59:46', '2023-05-10 20:59:47', '2023-05-10 20:59:48', '2023-05-10 20:59:49', '2023-05-10 20:59:50', '2023-05-10 20:59:51', '2023-05-10 20:59:52', '2023-05-10 20:59:53', '2023-05-10 20:59:54', '2023-05-10 20:59:55', '2023-05-10 20:59:56', '2023-05-10 20:59:57', '2023-05-10 20:59:58', '2023-05-10 20:59:59', '2023-05-10 21:00:00']
# 将字符串转换为datetime格式
data = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in data]
# 设置起止时间
start_time = datetime.strptime('2023-05-09 18:04:13', '%Y-%m-%d %H:%M:%S')
end_time = datetime.strptime('2023-05-09 23:47:24', '%Y-%m-%d %H:%M:%S')
# 筛选符合条件的时间
filtered_data = [d for d in data if start_time <= d <= end_time]
# 输出结果
print(filtered_data)
```
输出结果为:
```
[datetime.datetime(2023, 5, 10, 20, 37, 49), datetime.datetime(2023, 5, 10, 20, 37, 50), datetime.datetime(2023, 5, 10, 20, 37, 51), datetime.datetime(2023, 5, 10, 20, 37, 52), datetime.datetime(2023, 5, 10, 20, 37, 53), datetime.datetime(2023, 5, 10, 20, 37, 54), datetime.datetime(2023, 5, 10, 20, 37, 55), datetime.datetime(2023, 5, 10, 20, 37, 56), datetime.datetime(2023, 5, 10, 20, 37, 57), datetime.datetime(2023, 5, 10, 20, 37, 58), datetime.datetime(2023, 5, 10, 20, 37, 59), datetime.datetime(2023, 5, 10, 20, 38), datetime.datetime(2023, 5, 10, 20, 38, 1), datetime.datetime(2023, 5, 10, 20, 38, 2), datetime.datetime(2023, 5, 10, 20, 38, 3), datetime.datetime(2023, 5, 10, 20, 38, 4), datetime.datetime(2023, 5, 10, 20, 38, 5), datetime.datetime(2023, 5, 10, 20, 38, 6), datetime.datetime(2023, 5, 10, 20, 38, 7), datetime.datetime(2023, 5, 10, 20, 38, 8), datetime.datetime(2023, 5, 10, 20, 38, 9), datetime.datetime(2023, 5, 10, 20, 38, 10), datetime.datetime(2023, 5, 10, 20, 38, 11), datetime.datetime(2023, 5, 10, 20, 38, 12), datetime.datetime(2023, 5, 10, 20, 38, 13), datetime.datetime(2023, 5, 10, 20, 38, 14), datetime.datetime(2023, 5, 10, 20, 38, 15), datetime.datetime(2023, 5, 10, 20, 38, 16), datetime.datetime(2023, 5, 10, 20, 38, 17), datetime.datetime(2023, 5, 10, 20, 38, 18), datetime.datetime(2023, 5, 10, 20, 38, 19), datetime.datetime(2023, 5, 10, 20, 38, 20), datetime.datetime(2023, 5, 10, 20, 38, 21), datetime.datetime(2023, 5, 10, 20, 38, 22), datetime.datetime(2023, 5, 10, 20, 38, 23), datetime.datetime(2023, 5, 10, 20, 38, 24)]
```
阅读全文