[英]Create list of lists based on a condition
I have a string like below:我有一个如下所示的字符串:
result = """The following table provides the details.
acquired, by major class:
(US$ in millions) Customer relationships 15year $265
There is another line without space here.
Another table starts here:
(USS in millions) 2018 2017
Income (loss) from continuing operations $298 $129"""
I have to take all the sentences that contain more than 3 spaces and put them in a list of lists.我必须把所有包含超过 3 个空格的句子放在一个列表列表中。 Below is something I have tried so far:以下是我到目前为止尝试过的东西:
lines = result.splitlines()
table_list = []
for i in range(len(lines)):
if re.search(r' {3,}', lines[i]):
table_list.append(lines[i])
Resultant output of above code:以上代码的结果 output:
['(US$ in millions) Customer relationships 15year $265','(USS in millions) 2018 2017','Income (loss) from continuing operations $298 $129']
Expected Output:预计 Output:
[['(US$ in millions) Customer relationships 15year $265'],['(USS in millions) 2018 2017','Income (loss) from continuing operations $298 $129']]
Further explanation of output condition: Expected output should be a list of lists . output 条件的进一步说明: Expected output should be a list of lists 。 While iterating through each line, if there are consecutive sentences that contain 3 or more spaces between 2 words, all of these lines should be part of same list within the main list.在遍历每一行时,如果有连续的句子在 2 个单词之间包含 3 个或更多空格,则所有这些行都应该是主列表中同一列表的一部分。 If a line does not contain 3 or more spaces between 2 words, this breaks the chain.如果一行在 2 个单词之间不包含 3 个或更多空格,则链会中断。 If there is another line that contains 3 or more spaces between 2 words then this line becomes part of a new list inside the main list.如果有另一行在 2 个单词之间包含 3 个或更多空格,则该行将成为主列表中新列表的一部分。
Use itertools.groupby
with re.findall
:将itertools.groupby
与re.findall
一起使用:
from itertools import groupby
def has_spaces(str_):
return bool(re.findall("\s{3,}", str_))
[list(g) for k, g in groupby(result.splitlines(), key=has_spaces) if k]
Output: Output:
[['(US$ in millions) Customer relationships 15year $265'],
['(USS in millions) 2018 2017',
'Income (loss) from continuing operations $298 $129']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.