Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何重置for循环,使其从一开始就读取列表_Python_Loops - Fatal编程技术网

Python 如何重置for循环,使其从一开始就读取列表

Python 如何重置for循环,使其从一开始就读取列表,python,loops,Python,Loops,所以我有一个for循环,如果满足某些条件,它会删除列表中的元素。当它删除第二个元素时,它会继续从第二个元素开始循环。我的问题是,我希望它从一开始就开始循环,所以它会检查第一个项目的列表。可能吗 some_list = ["one", "two", "two", "one", "two", "three", "two"] for element in som

所以我有一个for循环,如果满足某些条件,它会删除列表中的元素。当它删除第二个元素时,它会继续从第二个元素开始循环。我的问题是,我希望它从一开始就开始循环,所以它会检查第一个项目的列表。可能吗

some_list = ["one", "two", "two", "one", "two", "three", "two"]

for element in some_list:
    if element == "one":
        if some_list.index(element) + 1 == two:
            some_list.remove(some_list.index(element) + 1)
下面是一些示例代码。我希望它检查是否有“1”,删除“2”,当它删除时,在开始时再次从“1”开始,删除“2”,再次转到“1”,转到第2个“1”,删除“2”,列表如下所示:

some_list = ["one", "one", "three", "two"]

您的方法有点低效,因为在循环结束时删除了所有“两个”元素,所以最好直接删除所有“两个”元素

for i, elem in enumerate(some_list):
    if elem == “two”:
        del some_list[i]
不过,为了回答这个问题,以下是我的解决方案:

i=0
while i != len(some_list):
    if some_list[i] == “one” and some_list[i+1] == “two”:
        del some_list[i+1]
    else:
         i += 1

您的方法有点低效,因为在循环结束时删除了所有“两个”元素,所以最好直接删除所有“两个”元素

for i, elem in enumerate(some_list):
    if elem == “two”:
        del some_list[i]
不过,为了回答这个问题,以下是我的解决方案:

i=0
while i != len(some_list):
    if some_list[i] == “one” and some_list[i+1] == “two”:
        del some_list[i+1]
    else:
         i += 1

删除“1”之后出现的所有“2”的算法:

>>> def del_two(some_list):
...     i = 0
...     while i < len(some_list):
...             if some_list[i] != 'one':
...                     i += 1
...                     continue
...             i += 1
...             while i < len(some_list) and some_list[i] == 'two':
...                     del some_list[i]

>>> some_list = ["one", "two", "two", "one", "two"]
>>> del_two(some_list)
>>> print(some_list)
['one', 'one']
>>def del_two(一些列表):
…i=0
…而我>>some_list=[“一”、“二”、“二”、“一”、“二”]
>>>德鲁二号(一些名单)
>>>打印(一些列表)
[‘一’、‘一’]

删除“一”之后出现的所有“二”的算法:

>>> def del_two(some_list):
...     i = 0
...     while i < len(some_list):
...             if some_list[i] != 'one':
...                     i += 1
...                     continue
...             i += 1
...             while i < len(some_list) and some_list[i] == 'two':
...                     del some_list[i]

>>> some_list = ["one", "two", "two", "one", "two"]
>>> del_two(some_list)
>>> print(some_list)
['one', 'one']
>>def del_two(一些列表):
…i=0
…而我>>some_list=[“一”、“二”、“二”、“一”、“二”]
>>>德鲁二号(一些名单)
>>>打印(一些列表)
[‘一’、‘一’]

我建议使用while循环,因为Python中的for循环一旦启动,就跟C++、java等

some_list = ["one", "two", "two", "one", "two"]
i = 0
while( i != (len(some_list) - 1)):
    if some_list[i] == "one":
        if some_list[i + 1] == 'two':
            del some_list[i + 1]
            i = 0
            continue
    i = i + 1
print(some_list)

我建议使用while循环,因为Python中的for循环一次启动,与C++、java等

some_list = ["one", "two", "two", "one", "two"]
i = 0
while( i != (len(some_list) - 1)):
    if some_list[i] == "one":
        if some_list[i + 1] == 'two':
            del some_list[i + 1]
            i = 0
            continue
    i = i + 1
print(some_list)

您可以尝试while循环而不是for循环,您需要的是:

some_list = ["one", "two", "two", "one", "two"]

i = 0
while i < len(some_list):
    element = some_list[i]
    if element == "one":
        idx = i + 1
        if idx < len(some_list) and some_list[idx] == "two":
            some_list.pop(idx)
            i -= 1
    i += 1

print(some_list)
some_list=[“一”、“二”、“二”、“一”、“二”]
i=0
而我
您可以尝试while循环而不是for循环,您需要的是:

some_list = ["one", "two", "two", "one", "two"]

i = 0
while i < len(some_list):
    element = some_list[i]
    if element == "one":
        idx = i + 1
        if idx < len(some_list) and some_list[idx] == "two":
            some_list.pop(idx)
            i -= 1
    i += 1

print(some_list)
some_list=[“一”、“二”、“二”、“一”、“二”]
i=0
而我
我不建议编辑一个您正在积极循环的迭代器。它可能会产生一些奇怪的行为。如果前面有
“一”
,那么最终结果是只删除
“两个”
,还是只删除所有
“两个”
?如果是后者,那么应该这样做:
[i for i in some_list if i!=“two”]
我不建议编辑您正在积极循环的迭代器。它可能会产生一些奇怪的行为。如果前面有
“one”
,最终结果是只删除
“two”
,还是只删除所有
“two”“
?如果是后者,那么应该这样做:
[i for i in some_list If i!=“two”]
但是我想删除“two”,只要它在“one”旁边,这个列表只是一个我应该做得更好的示例。我更新了我的响应,这样,如果当前元素后面的元素是“two”,迭代就不会增加。”.但是我想删除“2”,如果它在“1”旁边,这个列表只是一个例子,我应该做得更好。我更新了我的响应,这样,如果当前元素后面的元素是“2”,迭代次数就不会增加。