将日期范围拆分为两个列表



我想吐出以下日期列表:

Month=['1 October 2020 to 31 October 2020',
'1 October 2020 to 31 October 2020',
'1 October 2020 to 31 October 2020',
'1 October 2020 to 31 October 2020',
'1 October 2020 to 31 October 2020']

所需输出如下:

Month = [['1 October 2020', '31 October 2020'],
['1 October 2020', '31 October 2020'],
['1 October 2020','31 October 2020'],
['1 October 2020', '31 October 2020'],
['1 October 2020','31 October 2020']]

如何使用regex来完成此操作。

我使用了Month.str.split('to'),但它不能正常工作,因为October包含to,因此将October拆分为三个字符串。因此,我想我必须使用regex。实现这一目标的最佳方式是什么?

使用' to '作为分区,而不仅仅是to——无论如何,这与输入格式更匹配,因为如果在to上拆分,还需要去掉空白。

>>> [list(i.split(' to ') for i in Month)]
[[['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020'], ['1 October 2020', '31 October 2020']]]

最新更新