擅长:python、mysql、java
<blockquote>
<p>The question is: Is there a way to know if the list contains mixed True/False values without the if elif else test? Any built-in function to directly know I have mixed values in the list?</p>
</blockquote>
<p>可以创建lambda来执行此操作</p>
<pre><code>mixed = lambda x: len(set(x)) > 1
tf = [True, False, False, True]
f = [False, False, False]
t = [True, True, True]
mixed(tf) # returns true
mixed(t) # returns false
mixed(f) # returns false.
</code></pre>
<p>请注意,上述内容适用于任何列表,因为它仅在删除重复项后检查任何集合的长度。但我想你不会让不正确的价值观进入你的布尔人名单</p>