如果满足条件
我有以下python代码:
for i in range(large_number):
# compute a_i
if a_i is condition:
# Need to make change here for the file open
f = open('file.log', wb)
# more computations proceeding. Cannot use break
现在,由于我的代码复杂性,我只能通过
needs_fopen = False
for i in range(large_number):
# compute a_i
if a_i is condition:
needs_fopen = True
if needs_fopen is True:
f = open('file.log', wb)
我不想执行第二种方法才能实现打开文件。如何实现第一个方法,但只有满足条件时才打开文件一次?
I have the following Python code:
for i in range(large_number):
# compute a_i
if a_i is condition:
# Need to make change here for the file open
f = open('file.log', wb)
# more computations proceeding. Cannot use break
Now that I can achieve opening file only once by
needs_fopen = False
for i in range(large_number):
# compute a_i
if a_i is condition:
needs_fopen = True
if needs_fopen is True:
f = open('file.log', wb)
I do not want to do the second approach due to my code complexity. How to achieve the first method but open the file only once if the condition is satisfied?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
任何()
具有生成器理解:Use
any()
with a generator comprehension: