如果您对Python的in感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Python的in的详细内容,我们还将为您解答__contains__运算符返回布尔值,该布尔值既不
如果您对Python的in感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Python的in的详细内容,我们还将为您解答__contains__运算符返回布尔值,该布尔值既不是True也不是False的相关问题,并且为您提供关于AEM / CQ:选中复选框保存布尔值TRUE,如果我们取消选中,如何将布尔值保存为FALSE?、c# – 如果变量不是布尔值,则返回False、javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js、Java使用递归返回布尔值true的有价值信息。
本文目录一览:- Python的in(__contains__)运算符返回布尔值,该布尔值既不是True也不是False(python 返回布尔值)
- AEM / CQ:选中复选框保存布尔值TRUE,如果我们取消选中,如何将布尔值保存为FALSE?
- c# – 如果变量不是布尔值,则返回False
- javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js
- Java使用递归返回布尔值true
Python的in(__contains__)运算符返回布尔值,该布尔值既不是True也不是False(python 返回布尔值)
不出所料,空元组不包含1
>>> 1 in ()False
但是False
返回的值不等于False
>>> 1 in () == FalseFalse
换一种方式来看,in
运算符返回的abool
既不是也不True
是False
:
>>> type(1 in ())<type ''bool''>>>> 1 in () == True, 1 in () == False(False, False)
但是,如果对原始表达式加上括号,则会恢复正常行为
>>> (1 in ()) == FalseTrue
或其值存储在变量中
>>> value = 1 in ()>>> value == FalseTrue
在Python 2和Python 3中都观察到此行为。
你能解释发生了什么吗?
答案1
小编典典您正在遇到比较运算符链接;1 in () == False
也 没有 意思(1 in ()) == False
。
相反,比较是链接在一起的,该表达式的真正含义是:
(1 in ()) and (() == False)
因为(1 in ())
已经为false,所以False andsomething_else
将完全忽略链接表达式的后半部分(因为返回False
的值something_else
将是)。
请参阅比较表达式文档:
可以任意链接比较,例如
x < y <= z
与等效x < y and y <= z
,不同之处在于y
比较仅被评估一次(但在两种情况下z
都x< y
被发现为假,则根本不评估)。
对于记录,<
,>
,==
,>=
,<=
,!=
,is
,is not
,in
和not in
都是比较运算符(如本弃用<>
)。
通常,不要将其与布尔值进行比较;只是测试表达式本身。如果 必须
对布尔文字进行测试,则至少要使用括号和is
运算符,True
并且False
它们是单例的,就像None
:
>>> (1 in ()) is FalseTrue
当涉及整数时,这仍然变得更加令人困惑。Python的bool
类型是子类int
1。因此,False == 0
确实如此True ==1
。因此,可以想象到,您可以创建看起来理智的链接操作:
3 > 1 == True
是正确的,因为3 > 1
和1 == True
都正确。但是表达式:
3 > 2 == True
是错误的,因为2 == True
是错误的。
1是出于历史原因的子类;Python并不总是像C那样具有类型和具有布尔含义的重载整数。制作子类可使较旧的代码正常工作。bool``int``bool``bool
AEM / CQ:选中复选框保存布尔值TRUE,如果我们取消选中,如何将布尔值保存为FALSE?
<checkBox1 jcr:primaryType="cq:Widget" checked="false" defaultValue="false" fieldLabel="Sample" inputValue="true" name="./sample" checkBoxBoolTypeHint="{Boolean}true" type="checkBox" xtype="selection"> <listeners jcr:primaryType="nt:unstructured" check="function(isChecked){var panel = this.findParentByType('panel'); var fields = panel.find('name','./sample'); for (var i=0;i<fields.length; i++) {if (fields[i].xtype == 'hidden') { if (isChecked.checked) {fields[i].setdisabled(true);} else {fields[i].setdisabled(false);}}}}"/> </checkBox1> <hiddenCheckBox1 jcr:primaryType="cq:Widget" disabled="{Boolean}true" ignoreData="{Boolean}true" name="./sample" value="{Boolean}false" xtype="hidden"/>
如果我们选中/启用了复选框,则会显示属性“Sample”,如下所示
sample Boolean true(工作正常)
如果我们取消选中/禁用复选框,则表示未显示属性“Sample”
期望:如果我们取消选中/禁用复选框,我想显示Sample Boolean false
解决方法
例如,一个这样的后缀是@UseDefaultWhenMissing后缀,它应该是您正在寻找的.
从文档:
As described above,@DefaultValue only takes effect if no value is provided for a particular parameter. However,in some cases,such as HTML checkBoxes,this isn’t sufficient because the parameter isn’t submitted at all. To handle this scenario,you can use the @UseDefaultWhenMissing suffixed parameter.
<form method="POST" action="/content/page/first" enctype="multipart/form-data"> <input name="queryIgnoreNoise"type="checkBox" value="true"/> <input type="hidden" name="queryIgnoreNoise@DefaultValue" value="false"/> <input type="hidden" name="queryIgnoreNoise@UseDefaultWhenMissing" value="true"/> </form>
所以你在对话框定义中要做的就是添加两个额外的隐藏字段:
<checkBox1DefaultValue jcr:primaryType="cq:Widget" name="./sample@DefaultValue" value="{Boolean}false" xtype="hidden"/> <checkBox1UseDefaultWhenMissing jcr:primaryType="cq:Widget" name="./sample@UseDefaultWhenMissing" value="{Boolean}true" xtype="hidden"/>
密切关注字段的名称:
./sample@DefaultValue和./sample@UseDefaultWhenMissing.
它是复选框(示例)的名称加上两个必需后缀的名称:@DefaultValue和@UseDefaultWhenMissing.
您可以在Sling文档中阅读一些更好的后缀:
https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html
c# – 如果变量不是布尔值,则返回False
if (!Boolean.Parse(readValue("Useable"))) return true; return (defined.ContainsKey(key) || (key == "Useable"));
解决方法
bool isUseable; bool.TryParse(readValue("Useable"),out isUseable);
javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js
如何解决javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js?
我想知道 javascript 中是否有任何 object.contains
方法的实现,其工作方式类似于 Python 版本 object.__contains__()
的工作方式,因为我希望能够扫描整个对象嵌套对象以查看是否存在包含该方法与之进行比较的内容的键或值。我知道在 Javascript 中有其他方法可以做到这一点,例如使用 filter
和 some
,但它们对我不起作用。
person: {
name: ''Bob'',age: 40,items: {
cars: 4,house: 1,computer: 2
}
}
我需要扫描整个对象的东西,而不仅仅是它的第一级(这将是 name
、age
和 items
,如果您搜索 {{1 }} 你不会得到任何回应)。
解决方法
我不这么认为。一个天真的想法是
JSON.stringify(target).includes( JSON.stringify( search ) )
如果 search
不是字符串,它就不能很好地工作,因为它也会在字符串内部匹配。一个非天真的方法是这样的:
const contains = (obj,search) =>
Object.keys(obj).includes(search) ||
Object.values(obj).includes(search) ||
Object.values(obj)
.filter(it => typeof it === "object" && it !== null)
.some(it => contains(it,search));
,
我猜没有标准函数,但您肯定可以使用一些 typeof
和 Object.entries()
编写自定义递归函数。
let person = {
name: ''Bob'',age: 40,items: {
cars: 4,house: 1,computer: 2
}
};
function searchForTerm(obj,item){
for(let [key,val] of Object.entries(obj)){
if(key == item){
return true;
}
else if(typeof val === ''object'')
{
if(searchForTerm(val,item)){
return true;
}
}
else if(val == item){
return true;
}
}
return false;
}
console.log(searchForTerm(person,40));
console.log(searchForTerm(person,43));
console.log(searchForTerm(person,''Bob''));
console.log(searchForTerm(person,''cars''));
console.log(searchForTerm(person,''truck''));
我在第一种情况下使用 ==
,所以没有进行类型检查。
显然,这是一个初学者,您可以根据需要进行修改。但这应该能让你继续前进。
Java使用递归返回布尔值true
这是伪代码:
public boolean containsValue(Node node,Value v) { if (node.value.equals(v)) { return true; } containsValue(node.left,v); // <- search left tree containsValue(node.right,v); // <- search right tree return false; }
这总是返回false.
但是我不能这样做,因为第二个return语句是死代码:
return containsValue(node.left,v); return containsValue(node.left,v);
那么我该如何解决这个问题呢?
解决方法
public boolean containsValue(Node node,Value value){ int result = node.value.compareto(value); if(result == 0){ return true; }else if(result < 0){ if(node.left != null){ return containsValue(node.left,v); } return false; }else{ if(node.right != null){ return containsValue(node.right,v); } return false; } }
这将检查当前节点的值与参数值的比较方式.如果参数值较小,则返回左子项的结果(< 0),如果它们相同则返回true(== 0),如果pass by value较大则返回右子项的结果(大于0).这将继续,直到找到值或需要搜索的子项为空. 这种方法充分利用了二叉搜索树,因为它不检查所有变量并且平均效率为O(log(n)),而只是查看所有节点的平均效率为O(n),这是最差的. 边注:
获取具有该值的节点的方法基本上与您用node替换true,false替换为null,使用布尔替换Node
例:
public Node getNode(Node node,Value value){ int result = node.value.compareto(value); if(result == 0){ return node; }else if(result < 0){ if(node.left != null){ return containsValue(node.left,v); } return null; }else{ if(node.right != null){ return containsValue(node.right,v); } return null; } }
我们今天的关于Python的in和__contains__运算符返回布尔值,该布尔值既不是True也不是False的分享已经告一段落,感谢您的关注,如果您想了解更多关于AEM / CQ:选中复选框保存布尔值TRUE,如果我们取消选中,如何将布尔值保存为FALSE?、c# – 如果变量不是布尔值,则返回False、javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js、Java使用递归返回布尔值true的相关信息,请在本站查询。
本文标签: