Python:如果函数中没有使用if语句,则打印一些内容
我有一个函数,它有几个if语句,在条件为: 我想写一个if语句,如果函数中没有使用if语句,则打印“something” 可复制示例Python:如果函数中没有使用if语句,则打印一些内容,python,for-loop,if-statement,Python,For Loop,If Statement,我有一个函数,它有几个if语句,在条件为: 我想写一个if语句,如果函数中没有使用if语句,则打印“something” 可复制示例 def my_function(data): ### Constant Features Check x = pd.DataFrame({'value':data.nunique()}) for col in x.index: if x.loc[col, 'value'] == 1: print('C
def my_function(data):
### Constant Features Check
x = pd.DataFrame({'value':data.nunique()})
for col in x.index:
if x.loc[col, 'value'] == 1:
print('Column', col , 'is a constant value')
### lets say i have 10's of "for x in y: if statements "like this
### If none of are activated i want to print "something"
## if none of the print statements in the for loops print, print(something')
如果要打印
something
如果未激活任何If
条件,则可以使用标志来捕获所有If循环下的print语句是否打印某物
def my_function(data):
### Constant Features Check
x = pd.DataFrame({'value':data.nunique()})
anythingPrinted = False
for col in x.index:
if x.loc[col, 'value'] == 1:
print('Column', col , 'is a constant value')
anythingPrinted = True
### 100 such statements
# At the end of the function
if not anythingPrinted:
print("something")
你能多说一点你想解决的问题吗。如果你有100条
for/If
语句,那么你的方法可能有更深层次的问题,它需要更多的抽象。如果你有100条这样的if语句,那么你的代码很可能急需重构。我真的希望有一天你能告诉我们为什么你有100条for循环。@iGuanaut我已经重新评估了我的方法。谢谢你这么有见地。