python - Python:如果 else 循环表现不同
问题描述
我正在尝试对数据框的特定组合执行操作,但我的代码运行不符合我的想法,我认为我没有做一些明显的错误。
years_list = []
check1 = 1
check10 = 1
r =30
for p in combinations(test4.index,r):
den = np.mean(p)
num = np.std(p)
cv = num/den
if (den >= 561 and den <= 570 ) :
if(cv>=0.13 or cv <= 0.17 and check1):
check1=0
print("Combination 1 done")
elif(den>=391 and den <= 400):
if(cv>=0.13 or cv < 0.17 and check10):
check10 = 0
print("Combination 10 done")
if(check1+check10==0)
break
在这里,我正在更改 and 的值,check1
以便check10
循环0
在 if else 条件内只进入一次,如果两个条件都满足,那么它会中断循环。
Test4.index
是一个数据框,我猜应该是无关的信息。它的输出打印Combination 10 done
多次,这是不应该发生的。我是在犯一些非常根本的错误还是更深层次的错误,我无法弄清楚?
编辑:我粘贴缩进错误,现在我正确粘贴了。
解决方案
你的括号是错误的。
检查以下代码:
check1 = 0
if(True or True and check1):
print("valid")
else:
print("invalid")
输出:
有效的
如果要正确验证条件,请使用:
check1 = 0
if(True or True) and check1:
print("valid")
else:
print("invalid")
输出:
无效的
或者在你的情况下:
if(cv>=0.13 or cv < 0.17) and check10:
check10 = 0
print("Combination 10 done")
检查python中的运算符优先级以了解首先评估哪个运算符
推荐阅读
- c# - DisplayActionSheet() 不会将销毁按钮放在 Android 的顶部
- python - 使用 StringIO 从 pyscopg2 写入 PostgresSQL - 复制会随着时间的推移而变慢
- react-native - 在反应原生选项卡导航中导航到另一个屏幕时,如何明确退出一个屏幕?
- firebase - 如何使用 GameCenter Firebase 身份验证在 Unity 中访问身份验证令牌
- sass - SCSS - 悬停在多个元素上?
- javascript - 将函数值返回给反应组件
- json - 显示来自外部 url 的 json 数组中的数据,没有 api
- excel - 如何使用 VBA 将函数插入到单元格中?
- html - div 在表数据内的段落内。它有效吗?
- python - 按百分比计算每月和每周的入住率