用python填充列表

用python填充列表,python,regex,Python,Regex,我有一个列表,我想在python中的一个指定字符串之后/之前/之间分离元素 例如: 在指定字符串之后: 在指定字符串之间: 在指定字符串之前: 您可以通过以下方式访问列表中的列表: lst = list_of_list[0] 现在要搜索指定的字符串。你可以: lst.index('specified string') 它将返回字符串出现的索引。最后,您可以使用切片来获得所需的子列表 尝试这样做,如果您遇到问题,请将其发布在此处,我们将为您提供帮助。您可以通过以下方式访问列表中的列表: ls

我有一个列表,我想在python中的一个指定字符串之后/之前/之间分离元素

例如:

在指定字符串之后:

在指定字符串之间:

在指定字符串之前:


您可以通过以下方式访问列表中的列表:

lst = list_of_list[0]
现在要搜索指定的字符串。你可以:

lst.index('specified string') 
它将返回字符串出现的索引。最后,您可以使用切片来获得所需的子列表


尝试这样做,如果您遇到问题,请将其发布在此处,我们将为您提供帮助。

您可以通过以下方式访问列表中的列表:

lst = list_of_list[0]
现在要搜索指定的字符串。你可以:

lst.index('specified string') 
它将返回字符串出现的索引。最后,您可以使用切片来获得所需的子列表

尝试这样做,如果您遇到问题,请在此处发布,我们将为您提供帮助。

您可以使用函数和。我认为在性能方面,它可能比使用索引要好一点,至少如果您需要在两个字符串之间获取列表:

>>> from itertools import dropwhile, takewhile
>>> input1 = [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]
>>> input2 = [['aaaaa', 'bbbbb', 'cccc', 'specified string1', 'ddddd', 'eeeeee', 'ffffff', 'specified string2', 'qqqq', 'wwww', 'sssss']]
>>> input3 = [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]
>>> f = lambda x: x <> 'specified string'
>>> f1 = lambda x: x <> 'specified string1'
>>> f2 = lambda x: x <> 'specified string2'
>>>
>>> list(dropwhile(f, input1[0]))[1:]
['ddddd', 'eeeeee', 'ffffff']
>>> list(takewhile(f2, dropwhile(f1, input2[0])))[1:]
['ddddd', 'eeeeee', 'ffffff']
>>> list(takewhile(f, input3[0]))
['aaaaa', 'bbbbb', 'cccc']
您可以使用函数和。我认为在性能方面,它可能比使用索引要好一点,至少如果您需要在两个字符串之间获取列表:

>>> from itertools import dropwhile, takewhile
>>> input1 = [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]
>>> input2 = [['aaaaa', 'bbbbb', 'cccc', 'specified string1', 'ddddd', 'eeeeee', 'ffffff', 'specified string2', 'qqqq', 'wwww', 'sssss']]
>>> input3 = [['aaaaa', 'bbbbb', 'cccc', 'specified string', 'ddddd', 'eeeeee', 'ffffff']]
>>> f = lambda x: x <> 'specified string'
>>> f1 = lambda x: x <> 'specified string1'
>>> f2 = lambda x: x <> 'specified string2'
>>>
>>> list(dropwhile(f, input1[0]))[1:]
['ddddd', 'eeeeee', 'ffffff']
>>> list(takewhile(f2, dropwhile(f1, input2[0])))[1:]
['ddddd', 'eeeeee', 'ffffff']
>>> list(takewhile(f, input3[0]))
['aaaaa', 'bbbbb', 'cccc']

您可以轻松获取指定字符串的索引:

然后,您可以轻松提取所需的零件:

first = input[:boundaries[0]-1] # until the first occurrence
middle = input[boundaries[0]+1:boundaries[1]-1] # between the first and the second
last = input[boundaries[0]+1:] # from the last to the end

您可以轻松获取指定字符串的索引:

然后,您可以轻松提取所需的零件:

first = input[:boundaries[0]-1] # until the first occurrence
middle = input[boundaries[0]+1:boundaries[1]-1] # between the first and the second
last = input[boundaries[0]+1:] # from the last to the end

你能和我们分享一下你的尝试吗?目前看来,你只是要求我们为你做你的工作:你能和我们分享你的尝试吗?目前看来,你只是要求我们为你工作:可能比列表更好用的名字可能比列表更好用的名字