在 python 中,我有一个应该具有 只有一个真值(即 bool(value) is True)的列表。有什么聪明的办法来检查这个吗?现在,我只是遍历列表并手动检查:
def only1(l)
true_found = False
for v in l:
if v and not true_found:
true_found=True
elif v and true_found:
return False #"Too Many Trues"
return true_found
这看起来不够优雅,也不太像蟒蛇。有没有更聪明的方法来做到这一点?