我在一个列表中混合了True
、False
和None
。如果列表中所有的值都是None
,比如ListA=[None, None, None]
,我就必须返回None
。否则,如果我有布尔值和None
的混合,例如ListA=[True, True, False, None]
。我需要为这个列表返回True
,因为它有一个True
。
有没有另一种有效的方式来写这个逻辑?
到目前为止我的代码是:
any[listA] if list_A != None then None
发布于 2021-06-28 16:58:23
您可以尝试以下代码:
my_list=[True,None, None, None]
if(any(my_list)):
print("True")
else:
print("None")
如果my_list的所有项都为None.if,则返回None,而不返回True。
到目前为止,使用any()是更快、更有效的替代方法。
发布于 2021-06-28 18:29:34
试试这段代码。
is_bool_or_none = lambda some_list: True if (True in [type(element) == bool for element in some_list]) else None
print(is_bool_or_none(your_list_here))
https://stackoverflow.com/questions/68167018
复制相似问题