我需要在python中执行以下操作.我有一个字符串列表list,一个要搜索的字符串text,以及包含要打印的元素数量的变量x.我想迭代一下x.连续的元素,必要时list包裹在前面list.
首先,我需要找到的第一个元素中list有text一个子.然后我将从匹配元素list 之后的第一个元素开始,并继续遍历整个x连续元素,list必要时包裹.
我该怎么做?
x = 11
text = "string5"
list = ["string1", "string2", "string3", "string4", "string5", "string6", "string7"]
# not sure what to do here...
for elem in list:
if text in elem:
#iterate through list, begin with elem and get the next 11 elements
#once you've reached string7, start over with string1`
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我想最终看看以下11个元素:
string6
string7
string1 …Run Code Online (Sandbox Code Playgroud)