Python-根据条件调用函数 [英] Python - Call a Function on a Condition
本文介绍了Python-根据条件调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种简洁的方法可以在条件上调用函数。
I was wondering if there is a concise way to call a function on a condition.
我有这个:
if list_1 != []:
some_dataframe_df = myfunction()
我想知道是否有可能在三元运算符或类似的东西。
I'm wondering if it is possible to this in a ternary operator or something similiar.
如果我这样做
(some_dataframe_df = myfunction()) if list_1 != [] else pass
这不起作用。
推荐答案
您的代码很好。我建议的唯一更改是使用非空列表的固有真实性:
Your code is fine. The only change I suggest is to use the inherent Truthness of non-empty lists:
if list_1:
some_dataframe_df = myfunction()
如果您希望使用三元语句,它将写为:
If you wish to use a ternary statement it would be written as:
some_dataframe_df = myfunction() if list_1 else some_dataframe_df
但是,这既不简洁也不可读。
However, this is neither succinct nor readable.
这篇关于Python-根据条件调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文