Python数组从一个字复制到另一个字
所以我有一个数组,我想复制这个数组中的每个字符串,从它找到一个作为条件的单词开始,到另一个具有相同单词的字符串结束。例如,如果我有一个数组,如下所示:Python数组从一个字复制到另一个字,python,arrays,string,Python,Arrays,String,所以我有一个数组,我想复制这个数组中的每个字符串,从它找到一个作为条件的单词开始,到另一个具有相同单词的字符串结束。例如,如果我有一个数组,如下所示: ['abcde','Loop:atfg','xyzgh','blabla','blablable Loop','other thing'] 例如,我想搜索单词loop并将每个字符串复制到一个新数组中,直到它再次找到loop为止。所以它应该返回如下内容: ['Loop: atfg','xyzgh','blabla','blablable Loop'
['abcde','Loop:atfg','xyzgh','blabla','blablable Loop','other thing']
例如,我想搜索单词loop并将每个字符串复制到一个新数组中,直到它再次找到loop为止。所以它应该返回如下内容:
['Loop: atfg','xyzgh','blabla','blablable Loop']
任何帮助都将不胜感激,谢谢可能有更好的方法来处理这个问题,但我认为一个好的老式循环在这里最有效:
def word_copy(lst, search):
res = []
for item in lst:
if res:
res.append(item)
if search in item:
return res
elif search in item:
res.append(item)
可能有更好的方法来处理这个问题,但我认为一个好的老式循环在这里最有效:
def word_copy(lst, search):
res = []
for item in lst:
if res:
res.append(item)
if search in item:
return res
elif search in item:
res.append(item)
尝试这样做:
list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
if any("loop" in l for l in list):
尝试这样做:
list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
if any("loop" in l for l in list):
下面的代码查找包含您的
search\u str
start_index = my_list.index(next(e for e in my_list if search_str in e))
end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
要了解如何使用它,请执行以下操作:
my_list = ['trash', 'Loop: 1','2','3','4 Loop', 'more trash']
search_str = "Loop"
start_index = my_list.index(next(e for e in my_list if search_str in e))
end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
result = my_list[start_index:end_index + 1]
它可能看起来比多行循环有点奇怪,但它更像Python方式:下面的代码查找包含您的
搜索的第一个和第二个元素的索引
start_index = my_list.index(next(e for e in my_list if search_str in e))
end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
要了解如何使用它,请执行以下操作:
my_list = ['trash', 'Loop: 1','2','3','4 Loop', 'more trash']
search_str = "Loop"
start_index = my_list.index(next(e for e in my_list if search_str in e))
end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
result = my_list[start_index:end_index + 1]
它可能看起来比多行循环有点奇怪,但它更像Python的方式:在列表中循环,查找第一次出现,然后是第二次出现:
input = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
target = 'Loop'
start_index, end_index = None, None
for i in input:
if target in i:
if start_index is None:
start_index = input.index(i)
continue
end_index = input.index(i)
break
output = input[start_index : end_index + 1]
循环浏览列表,查找第一个匹配项,然后查找第二个匹配项:
input = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
target = 'Loop'
start_index, end_index = None, None
for i in input:
if target in i:
if start_index is None:
start_index = input.index(i)
continue
end_index = input.index(i)
break
output = input[start_index : end_index + 1]
在您的列表中:
list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
我认为您可以尝试这样做,以便找到阵列中的位置:
ixs = [i for i, word in enumerate(list) if word.startswith('Loop') or word.endswith('Loop')]
然后,您只需将列表切片:
res = list[ixs[0]:ixs[1]+1]
希望这能对您有所帮助。关于您的列表:
list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
我认为您可以尝试这样做,以便找到阵列中的位置:
ixs = [i for i, word in enumerate(list) if word.startswith('Loop') or word.endswith('Loop')]
然后,您只需将列表切片:
res = list[ixs[0]:ixs[1]+1]
希望这能对您有所帮助。按收益率迭代源代码列表一次:
i = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
def find_range(items):
start = False
for i in items:
if 'Loop' in i:
yield i
if start:
break
start = True
elif start:
yield i
print list(find_range(i))
按收益率迭代源列表一次:
i = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
def find_range(items):
start = False
for i in items:
if 'Loop' in i:
yield i
if start:
break
start = True
elif start:
yield i
print list(find_range(i))
我看到有一些奇特的单行解决方案。:)
但我喜欢蛮力游戏,它看起来更容易理解:
>>> my_list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
>>> search_str = "(Loop)+"
>>> out = []
>>> count = 0
>>> for s in my_list:
... if count == 2:
... break
... m = re.search(search_str, s)
... if m != None:
... count += 1
... if count >= 1:
... out.append(s)
...
>>> out
['Loop: atfg', 'xyzgh', 'blabla', 'blablable Loop']
>>>
我看到有一些奇特的单行解决方案。:)
但我喜欢蛮力游戏,它看起来更容易理解:
>>> my_list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
>>> search_str = "(Loop)+"
>>> out = []
>>> count = 0
>>> for s in my_list:
... if count == 2:
... break
... m = re.search(search_str, s)
... if m != None:
... count += 1
... if count >= 1:
... out.append(s)
...
>>> out
['Loop: atfg', 'xyzgh', 'blabla', 'blablable Loop']
>>>
注意OPs标准以-不包含开头和以-不包含结尾OPs标准以-不包含开头和以-不包含结尾这将执行if
当单词“loop”出现在列表的任何元素中时-这对OPs查询有何帮助?这将执行if
当单词“loop”出现时出现在列表的任何元素中-这对OPs查询有何帮助?但首先匹配的是什么:索引?因为这个变量似乎不知从何而来。会是start_索引吗?更新:我把它改为启动_索引,它成功了。谢谢但第一个匹配:索引是什么?因为这个变量似乎不知从何而来。会是start_索引吗?更新:我把它改为启动_索引,它成功了。谢谢我有点困惑,因为在开始时,如果start:break
,那么开始就是false。首次匹配循环时,“开始”将设置为true。当循环再次匹配时,start已经为true,因此请中断循环。由于if start:break
,但在其他方面很好:)在开始时,start为false,因此有点混乱。首次匹配循环时,“开始”将设置为true。当循环再次匹配时,“开始”已为true,因此请中断循环。