Python 在迭代列表时执行操作

Python 在迭代列表时执行操作,python,list,iteration,Python,List,Iteration,我正在迭代一个列表 xxx yyy **start word** xxx yyy zzz **stop word** break 我需要在另一个列表中添加开始和停止词之间的所有数据,我该怎么做 停止字在列表中出现几次。因此,当循环在其路径上找到第一个停止字时,应该停止追加 例如: list = [1,2,3 ... 1000] new_list = [] for i in list: # Once i = 5 I need to start appending i values to

我正在迭代一个列表

xxx
yyy
**start word**
xxx
yyy
zzz
**stop word** 
break
我需要在另一个列表中添加开始和停止词之间的所有数据,我该怎么做

停止字在列表中出现几次。因此,当循环在其路径上找到第一个停止字时,应该停止追加

例如:

list = [1,2,3 ... 1000]
new_list = []
for i in list:
    # Once i = 5 I need to start appending i values to new_list until i = 25.

您可以维护一个布尔值来指示何时开始追加和何时停止追加。为此,您可以编写如下代码-

old_list = ['axz','bbbdd','ccc','start','Hello World','Bye','end','ezy','foo']
another_list=[]

append_to_list = False     # Boolean to indicate if we should append current element
start_word = 'start'
end_word = 'end'
for element in old_list:
    if element == end_word :
        append_to_list = False
    if append_to_list :    # Appending to list if the Boolean is set
        another_list.append(element)
    if element == start_word :
        append_to_list = True


print(another_list)
    
输出:

这里,开始和结束是开始和停止词,您可以根据程序的开始和停止词修改它们

另一种可能的解决方案是获取起始词和终止词的索引,并将这些索引之间的元素存储到另一个列表中,如下所示-

old_list = ['axz','bbbdd','ccc','start','Hello World','Bye','end','ezy','foo']

start_idx = old_list .index("start")
stop_idx = old_list .index("end")

another_list = old_list[start_idx+1:stop_idx]

print(another_list)
    
输出:


希望这有帮助

您可以维护一个布尔值来指示何时开始追加和何时停止追加。为此,您可以编写如下代码-

old_list = ['axz','bbbdd','ccc','start','Hello World','Bye','end','ezy','foo']
another_list=[]

append_to_list = False     # Boolean to indicate if we should append current element
start_word = 'start'
end_word = 'end'
for element in old_list:
    if element == end_word :
        append_to_list = False
    if append_to_list :    # Appending to list if the Boolean is set
        another_list.append(element)
    if element == start_word :
        append_to_list = True


print(another_list)
    
输出:

这里,开始和结束是开始和停止词,您可以根据程序的开始和停止词修改它们

另一种可能的解决方案是获取起始词和终止词的索引,并将这些索引之间的元素存储到另一个列表中,如下所示-

old_list = ['axz','bbbdd','ccc','start','Hello World','Bye','end','ezy','foo']

start_idx = old_list .index("start")
stop_idx = old_list .index("end")

another_list = old_list[start_idx+1:stop_idx]

print(another_list)
    
输出:


希望这有帮助

如果能获得更多信息,那就太好了,但根据您提供的信息,您可以使用起始词和终止词的索引附加到新列表中:

list1 = ["xxx", "yyy", "start_word", "xxx", "yyy", "zzz", "end_word"]

a = list1.index("start_word")
b = list1.index("end_word")

list2 = []
list2.append(list1[a:b])

print(list2)
输出:
如果能获得更多信息,那就太好了,但是根据您提供的信息,您可以使用开始和结束词的索引来添加到新列表中:

list1 = ["xxx", "yyy", "start_word", "xxx", "yyy", "zzz", "end_word"]

a = list1.index("start_word")
b = list1.index("end_word")

list2 = []
list2.append(list1[a:b])

print(list2)
输出:
你能给我们看看你的代码吗?不清楚你想做什么。你能给我们看看你的代码吗?不清楚您要做什么。您应该使用list.extend来删除不必要的额外列表。如果附加一个列表片段,它将附加一个列表,而不是列表中的单个元素。另一方面,您可以直接使用切片列表,它已经是所需元素的列表,如果您将+1添加到开始索引中。@NathanThomas谢谢,很好的解决方案,它几乎解决了我的问题,但是这个停止词可能会出现在列表的前面,所以我需要程序在开始单词之后开始录制,在列表中第一个停止单词之后停止。我很抱歉没有向你提供那条信息,谢谢你,说得好!您应该使用list.extend来消除不必要的额外列表。如果附加一个列表片段,它将附加一个列表,而不是列表中的单个元素。另一方面,您可以直接使用切片列表,它已经是所需元素的列表,如果您将+1添加到开始索引中。@NathanThomas谢谢,很好的解决方案,它几乎解决了我的问题,但是这个停止词可能会出现在列表的前面,所以我需要程序在开始单词之后开始录制,在列表中第一个停止单词之后停止。我很抱歉没有向你提供那条信息,谢谢你,说得好!第二种解决方案基本上是内森解决方案的固定版本。@JanChristofterasa,没错!他的版本是在列表中添加一个列表,并使用起始元素,这不是OP的意图。所以我在回答中添加了正确的版本第一个版本正是我需要的,谢谢!第二种解决方案基本上是内森解决方案的固定版本。@JanChristofterasa,没错!他的版本是在列表中添加一个列表,并使用起始元素,这不是OP的意图。所以我在回答中添加了正确的版本第一个版本正是我需要的,谢谢!