Python 如何使if在for循环中只运行一次?
我试图根据一个条件将两个列表中的项目放在一起,以创建第三个列表作为输出。虽然我是个新手,但这相对简单。然而,我试图让循环的一部分只运行一次,而这正是我努力的方向。有办法做到这一点吗 数据来自文本数据的大数据框。但我创建了一个简化版的问题,试图更轻松地解决它(没有运气): 这使得:Python 如何使if在for循环中只运行一次?,python,python-3.x,Python,Python 3.x,我试图根据一个条件将两个列表中的项目放在一起,以创建第三个列表作为输出。虽然我是个新手,但这相对简单。然而,我试图让循环的一部分只运行一次,而这正是我努力的方向。有办法做到这一点吗 数据来自文本数据的大数据框。但我创建了一个简化版的问题,试图更轻松地解决它(没有运气): 这使得: [1, 2, 3, 3.5, 4, 3.5, 5] 如何得到以下结果 [1, 2, 3, 3.5, 4, 5] 您可以为此添加布尔标志: a = [1, 2, 3, 4, 5] A = [4, 5] b = []
[1, 2, 3, 3.5, 4, 3.5, 5]
如何得到以下结果
[1, 2, 3, 3.5, 4, 5]
您可以为此添加布尔标志:
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)
Edit:实际上,如果您的循环真的那么简单,那么其他答案就更优雅了,因为除了第一次执行之外,
if
和else
子句是相等的。但是,如果不是,我的代码就是正确的选择。您可以添加一个初始化为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)
你能用
不在中吗
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)
更改for循环中的逻辑的一种通用方法是手动声明迭代器,然后可以从循环中中断
,并在停止的地方继续
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, ...)
以上所有答案都是正确的。但我想补充一些解释。在这种情况下,如果需要在循环内部运行命令,最好使用额外的变量来跟踪是否执行了。用一个值初始化变量。在变量值保持不变之前,我们可以认为该行不会执行。使用这个额外的变量检查不会像使用“in”操作符那样增加程序的时间复杂度
我想在此添加一些示例。第一个使用True
或False
:
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)
使用整数
值:
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)
使用字符串
:
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)
使用列表
(奇怪的一个,但工作正常!):
使用集合和无跟踪变量
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))
这必须为每个循环迭代搜索'b'。根据数据大小,这可能会非常昂贵。我正要通过添加布尔标志进行编辑,但其他人已经添加了它作为答案。现在编辑它似乎不太公平:)我的真实循环更复杂,因为它嵌套在其他几个循环中,但这看起来非常漂亮和聪明。我必须尝试有效的方法,然后回来。非常感谢!这真是太好了!我建议您首先在和
中选中未运行
,以提高迭代器的使用效率。您必须在if子句中添加b.append(i)
,但是,您缺少4。在性能方面,这与带有标志的更简单解决方案相比如何?我觉得你必须重复很多代码才能实现this@Dux我添加了一个更一般的示例,说明如何在一般情况下对其进行调整。
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)
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]