python - 完成 IF 语句后,如何阻止代码继续进入特定部分?
问题描述
我有多个IF
要执行的语句,但是一旦满足任何 IF 语句的逻辑,我希望代码跳过任何 IF` 语句中未包含的特定部分。
我该怎么做呢?
当前如何设置代码:
if 1 in df.index:
if df.col1.isnull()[1] or (df.col1[1]==''):
[rest of the code]
if 2 in df.index:
if df.col1.isnull()[2] or (df.col1[2]==''):
[rest of the code]
if 3 in df.index:
if df.col1.isnull()[3] or (df.col1[3]==''):
[rest of the code]
[code I want to skip once any of the IF statements have been fulfilled]
[code I need to run regardless]
解决方案
您可以使用 if, elif 构造来构建逻辑,但是正如您所提到的,您想要跳过/运行不属于任何 if 块的代码的特定部分,这取决于在任何 if 块内完成的某些情况然后您可以使用标志在 if 块内翻转其值。
flag = True;
if condition1:
flag = False
if flag:
code to be skipped
推荐阅读
- python - 自定义状态代码应该放在 django 项目中的什么位置?
- grails - wslite 肥皂连接重置,soapui 正在使用相同的肥皂信封
- java - 部署到集群时平面图功能异常
- python - Python 脚本没有正确更改 chmod
- python - databricks - 挂载的 S3 - 如何获取文件元数据,如上次修改日期(Python)
- php - 我有一个关于 php 搜索关联数组的问题
- python - 如何使用 Pandas 和 Beautifulsoup 将 Web 抓取的数据加载到 Dataframe 中?
- node.js - 'TypeError: 需要 passport.js 后无法读取未定义的属性'isAuthenticated'
- python-3.x - 使用 cmd 模块:如何在“帮助”中记录子命令并与自动完成功能集成?
- azure - 如何重试和 ADF 管道执行直到满足条件