【发布时间】:2017-02-08 12:38:14
【问题描述】:
我看过这个:Split list into sublist based on index ranges
但我的问题略有不同。 我有一个清单
List = ['2016-01-01', 'stuff happened', 'details',
'2016-01-02', 'more stuff happened', 'details', 'report']
我需要根据日期将其拆分为子列表。基本上它是一个事件日志,但由于糟糕的数据库设计,系统将事件的单独更新消息连接到一个大字符串列表中。 我有:
Event_indices = [i for i, word in enumerate(List) if
re.match(date_regex_return_all = "(\d+\-\d+\-\d+",word)]
在我的例子中会给出:
[0,3]
现在我需要根据索引将列表拆分为单独的列表。所以对于我的例子来说,理想情况下我想得到:
[List[0], [List[1], List[2]]], [List[3], [List[4], List[5], List[6]] ]
所以格式是:
[event_date, [list of other text]], [event_date, [list of other text]]
还有一些极端情况,没有日期字符串,格式如下:
Special_case = ['blah', 'blah', 'stuff']
Special_case_2 = ['blah', 'blah', '2015-01-01', 'blah', 'blah']
result_special_case = ['', [Special_case[0], Special_case[1],Special_case[2] ]]
result_special_case_2 = [ ['', [ Special_case_2[0], Special_case_2[1] ] ],
[Special_case_2[2], [ Special_case_2[3],Special_case_2[4] ] ] ]
【问题讨论】:
-
格式
[event_date, [list of other text]]与输出[List[3], List[4]]不匹配,是[List[3], [List[4]]]吗?还有没有日期字符串的情况,输入@的期望输出是什么987654331@?[date, [thing1]], ["", thing2]或[date, thing1, thing2]? -
输入列表中的所有空字符串是否都被视为无日期字符串的情况?
-
修复了这个例子。我后来修改了这个例子,忘了修改它。` [date, [thing1]], ["", [thing2] ]` 是没有日期字符串的期望输出。是的,所有空都视为没有日期字符串
-
我还是不知道你对没有日期字符串的定义是什么,你能具体说明一下吗?
-
添加了特殊情况的示例输入和结果