如何在for循环中只运行一次if?

时间:2018-06-06 16:23:37

标签: python python-3.x

我试图根据条件将两个列表中的项目放在一起,以创建第三个列表作为输出。即使我是新手,这也相对简单。但是,我试图让循环的一部分只运行一次,这就是我奋斗的地方。有没有办法做到这一点?

数据来自大型文本数据DataFrame。但是我创建了一个问题的简化版本,试图更容易地解决它(没有运气):

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
for i in a:
    if i in A:
        b.append(3.5) # My aim is to make this line run only once
        b.append(i)
    else:
        b.append(i)
print(b)

这给出了:

[1, 2, 3, 3.5, 4, 3.5, 5]

如何获得以下结果?

[1, 2, 3, 3.5, 4, 5]

6 个答案:

答案 0 :(得分:4)

你可以为此添加一个布尔标志:

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
extend = True
for i in a:
    if i in A:
        if extend:
            b.append(3.5) # My aim is to make this line run only once
            extend = False
        b.append(i)
    else:
        b.append(i)
print(b)

编辑:实际上,如果您的循环真的 简单,那么其他答案会更优雅,因为除了第一次执行,ifelse条款是平等的。但是,如果不是,那么我的代码就是你要去的地方。

答案 1 :(得分:2)

您可以添加一个初始化为false的布尔值,然后在运行b.append(3.5)后直接将其设置为true。如果在if语句中检查此值,它将只运行一次。

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []

ran = False
for i in a:
    if i in A and not ran:
        b.append(3.5) # My aim is to make this line run only once
        ran = True
        b.append(i)
    else:
        b.append(i)
print(b)

答案 2 :(得分:2)

您可以使用not in吗?

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
for i in a:
    if i in A and 3.5 not in b:
        b.append(3.5)
        b.append(i)
    else:
        b.append(i)
print(b)

答案 3 :(得分:2)

更改for循环中逻辑的一种通用方法是手动声明迭代器,然后从循环中break继续,然后从中断处继续。

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []

iter_a = iter(a)

for i in iter_a:
    if i in A:
        b.append(3.5)
        b.append(i)
        break
    b.append(i)

b.extend(iter_a)

print(b) # [1, 2, 3, 3.5, 4, 5]

如果条件更复杂,并且一旦遇到您的代码就不会再次计算,那么这一点尤其有用。

使用函数,可以扩展到任何逻辑。

def do_something(*args):
    ...

def do_something_once(*args):
    ...

def condition(*args):
    ...

iterator = iter(...)

for i in iterator:
    if condition(i):
        do_something_once(i, ...)
        break
    do_something(i, ...)

for i in iterator:
    do_something(i, ...)

答案 4 :(得分:1)

以上所有答案都是正确的。但我想补充一些解释。在这种需要在循环内部运行命令的情况下,最好使用额外的变量来跟踪是否执行。使用值初始化变量。在变量值不变之前,我们可以认为该行未被执行。使用这个额外的变量检查不会增加程序中的时间复杂度,例如使用""操作

我想在这里添加一些例子。第一个使用TrueFalse

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
this_portion_is_executed=False
for i in a:
    if i in A and not this_portion_is_executed:
        b.append(3.5)
        b.append(i)
        this_portion_is_executed=True
    else:
        b.append(i)
print(b)

使用integer值:

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
this_portion_is_executed=1
for i in a:
    if i in A and not this_portion_is_executed==1:
        b.append(3.5) 
        b.append(i)
        this_portion_is_executed=2
    else:
        b.append(i)
print(b)

使用String

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
this_portion_is_executed="not executed"
for i in a:
    if i in A and not this_portion_is_executed=="not executed":
        b.append(3.5)
        b.append(i)
        this_portion_is_executed="executed"
    else:
        b.append(i)
print(b)

使用list(奇怪的一个,但正在工作!):<​​/ p>

a = [1, 2, 3, 4, 5]
A = [4, 5]

b = []
this_portion_is_executed_list=[]
for i in a:
    if i in A and not this_portion_is_executed: #because empty list is treated as false
        b.append(3.5) 
        b.append(i)
        this_portion_is_executed.append(3.5) #you can append any other value
    else:
        b.append(i)
print(b)

答案 5 :(得分:0)

使用集合而不使用跟踪变量,

a = [1, 2, 3, 4, 5]
A = [4, 5]

listU = [set(a), set(A)]
b = set.union(*listU)

res = []

for i in b:
    if 3.5 not in res:
        print("loop")
        res.append(3.5)
    res.append(i)
    
print(sorted(res))


loop #proof only runs once 
[1, 2, 3, 4, 5, 3.5]

[Program finished]
相关问题