如果 boolean 值列表为真,我将尝试运行一些函数。 我的列表由 boolean 函数组成。
list = [file.endswith('05',21,22), file.endswith('11',21,22),
file.endswith('17',21,22), file.endswith('21',21,22),
file.endswith('23',21,22)]
if any(True in list) == True:
# do something
目前,if 子句给我一个错误
if any(True in list) == True:
TypeError: 'bool' object is not iterable
不太确定如何修复它。
请您参考如下方法:
any期待一个可迭代的参数,然后它将运行它以查看其任何项目是否评估为 True。此外,表达式 True in list 返回一个不可迭代的 boolean 值:
>>> lst = [False, True, False]
>>> True in lst
True
>>>
要解决这个问题,您只需将列表传递给 any:
if any(list):
您还应该避免使用户定义的名称与内置名称之一相同。这样做会掩盖内置并使其在当前范围内无法使用。
