你能解释一下如何在python中运行吗? 我知道什么时候
x y and
0 0 0 (returns x)
0 1 0 (x)
1 0 0 (y)
1 1 1 (y)
在翻译中
>> lis = [1,2,3,4]
>> 1 and 5 in lis
输出为FALSE
但是,
>>> 6 and 1 in lis
输出为TRUE
它是如何运作的?
在这种情况下该怎么做在我的程序中我必须输入if条件只有当两个值都在列表中时?
答案 0 :(得分:7)
尽管有许多相反的论据,
XmlTextReader rd = new XmlTextReader(@"Test.xml");
string dnume = "", dcadru = "", snume = "",snota="", element = "";
while ( rd.Read() )
{
switch (rd.NodeType)
{
case XmlNodeType.Element:
element = rd.Name;
break;
case XmlNodeType.Text:
if (element == "Disciplina")
{
dnume = rd.GetAttribute("nume");
dcadru = rd.GetAttribute("cadru");
}
else
if (element == "Student")
{
}
break;
case XmlNodeType.EndElement:
if (rd.Name == "Student1")
{
MessageBox.Show("");
}
break;
}
}
rd.Close();
装置
6 and 1 in lis
不意味着:
6 and (1 in lis)
Maroun Maroun在评论中链接到的{{3}}表示(6 and 1) in lis
的优先级低于and
。
你可以这样测试:
in
如果这意味着0 and 1 in [0]
,那么它将评估为true,因为(0 and 1) in [0]
位于0
。
如果这意味着[0]
,那么它将评估为0 and (1 in [0])
,因为0
为假。
评估为0
。
答案 1 :(得分:2)
这一行
lis = [1,2,3,4]
1 and 5 in lis
相当于
lis = [1,2,3,4]
1 and (5 in lis)
由于bool(1)
是True
,所以就像写
lis = [1,2,3,4]
True and (5 in lis)
现在因为lis
中的5 不,我们得到True
和False
,即False
。
答案 2 :(得分:1)
您的陈述1 and 5 in lis
评估如下:
5 in lis --> false
1 and false --> false
和6 and 1 in lis
的评估如下:
1 in lis --> true
6 and true --> true
最后一个语句的计算结果为true,因为0以外的任何数字都是true
在任何情况下,这都是验证列表中是否存在多个值的错误方法。您可以使用all
运算符来post:
all(x in lis for x in [1, 5])